<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Think\Log;
use Think\Upload;
use Home\Controller\AppResult;
class RecordController extends AuthController {
    public function __construct(){
        parent::__construct();

    }
    public function index(){
        $this->display();
    }


    public function recordList(){

        $s_time = date("Y-m-d",time())." 00:00:00";
        $e_time = date("Y-m-d",time())." 23:59:59";
        if(IS_AJAX){
            $start = isset($_REQUEST['start'])?$_REQUEST['start']:0;
            $length = isset($_REQUEST['length'])?$_REQUEST['length']:10;
            $SearchMode = isset($_REQUEST['SearchMode'])?$_REQUEST['SearchMode']:0;
            $search_key = $_REQUEST['search_key'];
            $has_video = $_REQUEST['has_video'];
            $stime = isset($_REQUEST['stime'])?$_REQUEST['stime']:1;
            $stime = empty($stime)?0:$stime;
            $InOut = $_REQUEST['InOut'];
            $chk_CN = $_REQUEST['chk_CN'];
			$remark = trim($_REQUEST['remark']);
            $datetimepicker_start = isset($_REQUEST['datetimepicker_start'])?$_REQUEST['datetimepicker_start']:$s_time;
            $datetimepicker_end = isset($_REQUEST['datetimepicker_end'])?$_REQUEST['datetimepicker_end']:$e_time;
            //$name = $_REQUEST['search']['value'];
            $m = M("rec_cdrinfo");
            $result=$m->find(1);
            if(false === $result){
                $output['error'] = 1;
                $output['errorThrown'] = "数据库连接失败";
                echo json_encode($output);exit;
            }
            $m_bak = M("rec_bakinfo");

            $sEcho = $_REQUEST['sEcho']; // DataTables 用来生成的信息
            $output['sEcho'] = $sEcho;
            $field="N_SN,N_ChannelID,D_StartTime,D_StopTime,V_VoiceFile,N_FileFormat,";
            $field.="N_CallDirection,V_Caller,V_Called,V_Ext,N_BackUp,N_Lock,N_Alarm,";
            $field.="N_AvoidRec,N_DeleteFlag,N_Locker,V_Path,V_NetPath,";
            $field.="V_Diverter,V_Diverted,N_IsTalk,ReMark ";


            $where  =  " D_StartTime between '{$datetimepicker_start}' and '$datetimepicker_end' ";

			$RecNum=getStationRecNum();//有权号码，有权工作站的录音号码
            $RecNumArr = explode(',',$RecNum);
            $strRecStr ="";
            if(!empty($RecNumArr) && $RecNumArr !=''){
                foreach ($RecNumArr as $v){
                    if($strRecStr == ''){
                        $strRecStr =  "'".$v."'";
                    }
                    else{
                        $strRecStr = $strRecStr.",". "'".$v."'";
                    }
                }
            }

			if($strRecStr !="" && $strRecStr !="''"){
				$where.=" and V_Ext in({$strRecStr})";	//用户有权号码过滤记录
			}

            if(trim($chk_CN) !='' && $chk_CN != 'null'){
                $where  .= " and N_ChannelID in ($chk_CN)";
            }

            if($search_key != ""){//通话号码(姓名)的取值，0则号码，1则姓名
                if($SearchMode ==1){
                    $numArr=getNumByName($search_key);
                    if(!$numArr || empty($numArr)){
                        $where .= ' and N_SN = -1';//姓名没匹配到号码，直接设置一个不成立的条件
                    }else{
						if($InOut !=''){
							if($InOut == 2){
                                $where  .= " and (";
                                foreach($numArr as $k=>$v){
                                    if($k == 0){
                                        $where  .= " v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                    else{
                                        $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }

                                }
                                $where  .= ")";

							}
							elseif($InOut == 1){
                                $where  .= " and ((";
                                foreach($numArr as $k=>$v){
                                    if($k == 0){
                                        $where  .= "   v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                    else{
                                        $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                }
                                $where  .= ") and N_CallDirection = 1) ";
							}
							elseif($InOut == 0){
                                $where  .= " and ((";
                                foreach($numArr as $k=>$v){
                                    if($k == 0){
                                        $where  .= "   v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                    else{
                                        $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                }
                                $where  .= ") and N_CallDirection = 0) ";
							}
						}
                        else{
                            $where  .= " and (";
                            foreach($numArr as $k=>$v){
                                if($k == 0){
                                    $where  .= " v_caller  = '{$v}' or v_called  = '{$v}' ";
                                }
                                else{
                                    $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}' ";
                                }

                            }
                            $where  .= ")";
                        }
                    }
                }else{
                    if($InOut !=''){
                        if($InOut == 2){
                            $where  .= "  and (v_caller  like '{$search_key}%'  or  v_called  like '{$search_key}%' )    ";
                        }
                        elseif($InOut == 1){
                            $where  .= " and ((v_caller  like '{$search_key}%'  or  v_called  like '{$search_key}%' ) and N_CallDirection = 1)   ";
                        }
                        elseif($InOut == 0){
                            $where  .= " and ((v_caller  like '{$search_key}%'  or  v_called  like '{$search_key}%' )  and N_CallDirection = 0)  ";
                        }
                    }
                    else{
                        $where  .= " and (v_caller  like '{$search_key}%'  or  v_called  like '{$search_key}%' )  ";
                    }

                }
                //echo $where;die;
            }
            //按拨叫方向查询
            if($InOut != 2 && $InOut !=''){
                $where .= " and N_CallDirection = '{$InOut}' ";
            }
            //echo $where;die;
            if ($has_video == 1) {//只查有录象的记录

                $sql = "(select `recid` from tab_ved_cdrinfo) union (select `recid` from tab_ved_bakinfo order by `recid`) ";
                $res = M()->query($sql);
                $recid = "";
                if($res){
                    foreach ($res as $v){
                        if($recid == ""){
                            $recid = $v['recid'];
                        }
                        else
                        {
                            $recid = $recid .",".$v['recid'];
                        }
                    }
                }
                if(!empty($recid)){
                   $where .= " and N_SN in ($recid)";
                }
                else{
                   $where .= " and N_SN = -1 ";
                }
            }


			if($remark != ''){
                $where .= " and ReMark like '%{$remark}%' ";
            }
            $where.=" and  (unix_timestamp(D_StopTime)-unix_timestamp(D_StartTime))>=$stime ";
			//echo $where;exit;
            $CntNoBackup=$m->field("count(*) cnt")->where($where)->find();	//未备份部分数据
            $CntNoBackup=$CntNoBackup['cnt'];
            $CntBackup = $m_bak->field("count(*) cnt")->where($where)->find();	//已备份部分数据
            $CntBackup=$CntBackup['cnt'];
            $total = (int)$CntNoBackup + (int)$CntBackup;
            $rs = array();
            if($CntBackup==0)
            {
                //无已备份数据，读取未备份数据即可
                $rs= $m->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
            }
            else
            {
                if($CntNoBackup==0)
                {
                    //有已备份数据，无未备份数据。读取已备份份数据即可
                    $rs= $m_bak->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
                }
                else
                {
                    //未备份和已备份都有，需要读取两张表
                    //计算未备份数据有多少页$NotBak_pagecnt
                    if($CntNoBackup % $length==0){
                        $NotBak_pagecnt=($CntNoBackup / $length);
                    }else{
                        $NotBak_pagecnt=(int)($CntNoBackup/$length)+1;
                    }
                    $NumNotShowed = $CntNoBackup - $start;	//未备份数据还有多少条未显示

                    if($NumNotShowed>0){
                        if($NumNotShowed >$length || $NumNotShowed == $length)
                        {
                            $rs= $m->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
                        }
                        else
                        {
                            //未备份数据不足一页的数据，需要同时读取备份与未备份数据
                            $rsNoBak= $m->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
                            $rsBak= $m_bak->field($field)->where($where)->limit(0,($length-$NumNotShowed))->order("D_StartTime desc")->select();
                            $rs= array_merge($rsNoBak,$rsBak);
                        }
                   }
                   else
                   {
                       //$NumNotShowed=0或者$NumNotShowed<0,则未备份录音已经显示完毕，仅查询备份表(tab_rec_bakinfo)即可
                       $rs= $m_bak->field($field)->where($where)->limit($start-$CntNoBackup,$length)->order("D_StartTime desc")->select();
                   }
                }
            }

            if($rs){
                foreach ($rs as $k=>$v)
                {
                    $fileplay ="<i class='fa fa-volume-up  fa-2x' style=\"cursor:pointer\" onclick=\"play('".$v["n_sn"]."','".$v["v_voicefile"]."')\"></i>";
                    $n = getCircfgByid($v['n_channelid']);
					$rs[$k]['n_channelid'] = $n['chno'];
                    $rs[$k]['n_channelinfo'] = $n['chname'];
                    $rs[$k]['v_caller'] = getNameByPhoneNum($v['v_caller']);
                    $rs[$k]['v_called'] = getNameByPhoneNum($v['v_called']);
                    $rs[$k]['v_ext'] = getNameByPhoneNum($v['v_ext']);
                    $rs[$k]['n_calldirection'] = $v['n_calldirection']==1?'拨出':'来电';
                    $rs[$k]['longtime'] = DateDiff($v['d_starttime'],$v['d_stoptime']);
                    $rs[$k]['v_voicefileplay'] = $fileplay;
                    $rs[$k]['local_video'] = getVideoByRecID($v['n_sn'],0);
                    $rs[$k]['remote_video'] = getVideoByRecID($v['n_sn'],1);
                    //$rs[$k]['translate'] = get_translate($v['n_sn']);
                    $str = "";
                    if($v["n_lock"] == 1){
                        $str.="<i class='fa fa-lock fa-2x' alt='已锁定'";
                    }
                    else{
                        $str.="<i class='fa fa-unlock fa-2x' alt='未锁定'";
                    }

                    if(canLock()==1){
                        $str.="   style='cursor:pointer;'";
                        $str.=" onclick='lock(".$v["n_sn"].",".$v["n_lock"].",".$v["n_backup"].",this);'";
                    }else{
                        $str.=($v["n_lock"]==1)?" alt='已锁定'":" alt='未锁定'";
						$str.=" onclick='lock(".$v["n_sn"].",".$v["n_lock"].",".$v["n_backup"].",this);'";
                    }
                    $str.=" id='img_lock_".$v["n_sn"]."'";
                    $str.=" /></i>";
                    $rs[$k]['n_lock_info'] = $str;

                    $str = "";
                    if($v["remark"]=="" || $v["remark"]==NULL){
                        $icoRemark="<img id='ico_".$v["n_sn"]."' src='/Public/assets/images/addRemark.gif' />";
                    }else{
                        $icoRemark="<img id='ico_".$v["n_sn"]."' src='/Public/assets/images/Remark.gif' />";
                    }
                    $str.="<span align='center' data-rm='".$v["remark"]."' onclick=\"Remark('".$v["n_sn"]."','".$v["n_backup"]."',this)\" style='cursor:pointer' ";
                    $str.=" title=\"".$v["remark"]."\" >{$icoRemark}</span>";
                    $rs[$k]['remark_info'] = $str;
                }
            }


            $output['aaData'] = $rs;
            $output['iTotalDisplayRecords'] = $total;    //如果有全局搜索，搜索出来的个数
            $output['iTotalRecords'] = $total; //总共有几条数据
            echo json_encode($output); //最后把数据以json格式返回
            die;
        }

        //列出所有通道
        $rsChno = M('drcu_circfg')->field("any_value(chno) chno,any_value(dispchno) dispchno")->group("chno")->order('dispchno')->select();
        if($rsChno){
            foreach ($rsChno as $key => $value) {
                 $rsChno[$key]['channelname'] = $this->getIDByName($value['dispchno']);
            }
        }
        $userbrowser = $_SERVER['HTTP_USER_AGENT'];


		$this->assign("usingie",$usingie);
        $this->assign("rsChno",$rsChno);
        $this->assign("datetimepicker_start",$s_time);
        $this->assign("datetimepicker_end",$e_time);
        $this->assign("CurrentPage",'recordList');
		if ( preg_match( '/MSIE/i', $userbrowser ) || preg_match('/Trident/i', $userbrowser) ) {
            $this->display("recordList_ie");
        } else {
            $this->display();
        }
    }

    public function thirdRecordList(){

        $s_time = date("Y-m-d",time())." 00:00:00";
        $e_time = date("Y-m-d",time())." 23:59:59";
        if(IS_AJAX){
            $start = isset($_REQUEST['start'])?$_REQUEST['start']:0;
            $length = isset($_REQUEST['length'])?$_REQUEST['length']:10;
            $SearchMode = isset($_REQUEST['SearchMode'])?$_REQUEST['SearchMode']:0;
            $search_key = $_REQUEST['search_key'];
            $has_video = $_REQUEST['has_video'];
            $stime = isset($_REQUEST['stime'])?$_REQUEST['stime']:1;
            $stime = empty($stime)?0:$stime;
            $InOut = $_REQUEST['InOut'];
            $chk_CN = $_REQUEST['chk_CN'];
            $remark = trim($_REQUEST['remark']);
            $datetimepicker_start = isset($_REQUEST['datetimepicker_start'])?$_REQUEST['datetimepicker_start']:$s_time;
            $datetimepicker_end = isset($_REQUEST['datetimepicker_end'])?$_REQUEST['datetimepicker_end']:$e_time;
            //$name = $_REQUEST['search']['value'];
            $m = M("rec_cdrinfo");
            $result=$m->find(1);
            if(false === $result){
                $output['error'] = 1;
                $output['errorThrown'] = "数据库连接失败";
                echo json_encode($output);exit;
            }
            $m_bak = M("rec_bakinfo");

            $sEcho = $_REQUEST['sEcho']; // DataTables 用来生成的信息
            $output['sEcho'] = $sEcho;
            $field="N_SN,N_ChannelID,D_StartTime,D_StopTime,V_VoiceFile,N_FileFormat,";
            $field.="N_CallDirection,V_Caller,V_Called,V_Ext,N_BackUp,N_Lock,N_Alarm,";
            $field.="N_AvoidRec,N_DeleteFlag,N_Locker,V_Path,V_NetPath,";
            $field.="V_Diverter,V_Diverted,N_IsTalk,ReMark ";


            $where  =  " D_StartTime between '{$datetimepicker_start}' and '$datetimepicker_end' ";

            $RecNum=getStationRecNum();//有权号码，有权工作站的录音号码
            $RecNumArr = explode(',',$RecNum);
            $strRecStr ="";
            if(!empty($RecNumArr) && $RecNumArr !=''){
                foreach ($RecNumArr as $v){
                    if($strRecStr == ''){
                        $strRecStr =  "'".$v."'";
                    }
                    else{
                        $strRecStr = $strRecStr.",". "'".$v."'";
                    }
                }
            }

            if($strRecStr !="" && $strRecStr !="''"){
                $where.=" and V_Ext in({$strRecStr})";	//用户有权号码过滤记录
            }

            if(trim($chk_CN) !='' && $chk_CN != 'null'){
                $where  .= " and N_ChannelID in ($chk_CN)";
            }

            if($search_key != ""){//通话号码(姓名)的取值，0则号码，1则姓名
                if($SearchMode ==1){
                    $numArr=getNumByName($search_key);
                    if(!$numArr){
                        $where .= ' and N_SN = -1';//姓名没匹配到号码，直接设置一个不成立的条件
                    }else{
                        if($InOut !=''){
                            if($InOut == 2){
                                $where  .= " and (";
                                foreach($numArr as $k=>$v){
                                    if($k == 0){
                                        $where  .= "   v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                    else{
                                        $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }

                                }
                                $where  .= ")";

                            }
                            elseif($InOut == 1){
                                $where  .= " and ((";
                                foreach($numArr as $k=>$v){
                                    if($k == 0){
                                        $where  .= "   v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                    else{
                                        $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                }
                                $where  .= ") and N_CallDirection = 1) ";
                            }
                            elseif($InOut == 0){
                                $where  .= " and ((";
                                foreach($numArr as $k=>$v){
                                    if($k == 0){
                                        $where  .= "   v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                    else{
                                        $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}' ";
                                    }
                                }
                                $where  .= ") and N_CallDirection = 0) ";

                            }
                        }
                        else{
                            $where  .= " and (";
                            foreach($numArr as $k=>$v){
                                if($k == 0){
                                    $where  .= "   v_caller  = '{$v}' or v_called  = '{$v}' ";
                                }
                                else{
                                    $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}' ";
                                }
                            }
                            $where  .= ")";
                        }
                    }
                }else{
                    if($InOut !=''){
                        if($InOut == 2){
                            $where  .= "  and (v_caller  like '{$search_key}%'  or  v_called  like '{$search_key}%' ) ";
                        }
                        elseif($InOut == 1){
                            $where  .= " and ((v_caller  like '{$search_key}%'  or  v_called  like '{$search_key}%' ) and N_CallDirection = 1)   ";
                        }
                        elseif($InOut == 0){
                            $where  .= " and ((v_caller  like '{$search_key}%'  or  v_called  like '{$search_key}%' )  and N_CallDirection = 0)  ";
                        }
                    }
                    else{
                        $where  .= " and (v_caller  like '{$search_key}%'  or  v_called  like '{$search_key}%' )  ";
                    }

                }
                //echo $where;die;
            }
            //按拨叫方向查询
            if($InOut != 2 && $InOut !=''){
                //$where .= " and N_CallDirection = '{$InOut}' ";
            }
            //echo $where;die;
            if ($has_video == 1) {//只查有录象的记录

                $sql = "(select `recid` from tab_ved_cdrinfo) union (select `recid` from tab_ved_bakinfo) order by `recid` ";
                $res = M()->query($sql);
                $recid = "";
                if($res){
                    foreach ($res as $v){
                        if($recid == ""){
                            $recid = $v['recid'];
                        }
                        else
                        {
                            $recid = $recid .",".$v['recid'];
                        }
                    }
                }
                if(!empty($recid)){
                    $where .= " and N_SN in ($recid)";
                }
                else{
                    $where .= " and N_SN = -1 ";
                }
            }


            if($remark != ''){
                $where .= " and ReMark like '{$remark}%' ";
            }
            $where.=" and  (unix_timestamp(D_StopTime)-unix_timestamp(D_StartTime))>=$stime ";
            //echo $where;exit;
            $CntNoBackup=$m->field("count(*) cnt")->where($where)->find();	//未备份部分数据
            $CntNoBackup=$CntNoBackup['cnt'];
            $CntBackup = $m_bak->field("count(*) cnt")->where($where)->find();	//已备份部分数据
            $CntBackup=$CntBackup['cnt'];
            $total = (int)$CntNoBackup + (int)$CntBackup;
            $rs = array();
            if($CntBackup==0)
            {
                //无已备份数据，读取未备份数据即可
                $rs= $m->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
            }
            else
            {
                if($CntNoBackup==0)
                {
                    //有已备份数据，无未备份数据。读取已备份份数据即可
                    $rs= $m_bak->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
                }
                else
                {
                    //未备份和已备份都有，需要读取两张表
                    //计算未备份数据有多少页$NotBak_pagecnt
                    if($CntNoBackup % $length==0){
                        $NotBak_pagecnt=($CntNoBackup / $length);
                    }else{
                        $NotBak_pagecnt=(int)($CntNoBackup/$length)+1;
                    }
                    $NumNotShowed = $CntNoBackup - $start;	//未备份数据还有多少条未显示

                    if($NumNotShowed>0){
                        if($NumNotShowed >$length || $NumNotShowed == $length)
                        {
                            $rs= $m->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
                        }
                        else
                        {
                            //未备份数据不足一页的数据，需要同时读取备份与未备份数据
                            $rsNoBak= $m->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
                            $rsBak= $m_bak->field($field)->where($where)->limit(0,($length-$NumNotShowed))->order("D_StartTime desc")->select();
                            $rs= array_merge($rsNoBak,$rsBak);
                        }
                    }
                    else
                    {
                        //$NumNotShowed=0或者$NumNotShowed<0,则未备份录音已经显示完毕，仅查询备份表(tab_rec_bakinfo)即可
                        $rs= $m_bak->field($field)->where($where)->limit($start-$CntNoBackup,$length)->order("D_StartTime desc")->select();
                    }
                }
            }

            if($rs){
                foreach ($rs as $k=>$v)
                {
                    $fileplay ="<i class='fa fa-volume-up  fa-2x' style=\"cursor:pointer\" onclick=\"play('".$v["n_sn"]."','".$v["v_voicefile"]."')\"></i>";
                    $n = getCircfgByid($v['n_channelid']);
                    $rs[$k]['n_channelid'] = $n['chno'];
                    $rs[$k]['n_channelinfo'] = $n['chname'];
                    $rs[$k]['v_caller'] = getNameByPhoneNum($v['v_caller']);
                    $rs[$k]['v_called'] = getNameByPhoneNum($v['v_called']);
                    $rs[$k]['v_ext'] = getNameByPhoneNum($v['v_ext']);
                    $rs[$k]['n_calldirection'] = $v['n_calldirection']==1?'拨出':'来电';
                    $rs[$k]['longtime'] = DateDiff($v['d_starttime'],$v['d_stoptime']);
                    $rs[$k]['v_voicefileplay'] = $fileplay;
                    $rs[$k]['local_video'] = getVideoByRecID($v['n_sn'],0);
                    $rs[$k]['remote_video'] = getVideoByRecID($v['n_sn'],1);
                    //$rs[$k]['translate'] = get_translate($v['n_sn']);
                    $str = "";
                    if($v["n_lock"] == 1){
                        $str.="<i class='fa fa-lock fa-2x' alt='已锁定'";
                    }
                    else{
                        $str.="<i class='fa fa-unlock fa-2x' alt='未锁定'";
                    }

                    if(canLock()==1){
                        $str.="   style='cursor:pointer;'";
                        $str.=" onclick='lock(".$v["n_sn"].",".$v["n_lock"].",".$v["n_backup"].",this);'";
                    }else{
                        $str.=($v["n_lock"]==1)?" alt='已锁定'":" alt='未锁定'";
                        $str.=" onclick='lock(".$v["n_sn"].",".$v["n_lock"].",".$v["n_backup"].",this);'";
                    }
                    $str.=" id='img_lock_".$v["n_sn"]."'";
                    $str.=" /></i>";
                    $rs[$k]['n_lock_info'] = $str;

                    $str = "";
                    if($v["remark"]=="" || $v["remark"]==NULL){
                        $icoRemark="<img id='ico_".$v["n_sn"]."' src='/Public/assets/images/addRemark.gif' />";
                    }else{
                        $icoRemark="<img id='ico_".$v["n_sn"]."' src='/Public/assets/images/Remark.gif' />";
                    }
                    $str.="<span align='center' data-rm='".$v["remark"]."' onclick=\"Remark('".$v["n_sn"]."','".$v["n_backup"]."',this)\" style='cursor:pointer' ";
                    $str.=" title=\"".$v["remark"]."\" >{$icoRemark}</span>";
                    $rs[$k]['remark_info'] = $str;
                }
            }


            $output['aaData'] = $rs;
            $output['iTotalDisplayRecords'] = $total;    //如果有全局搜索，搜索出来的个数
            $output['iTotalRecords'] = $total; //总共有几条数据
            echo json_encode($output); //最后把数据以json格式返回
            die;
        }

        //列出所有通道
        $rsChno = M('drcu_circfg')->field("any_value(chno) chno,any_value(dispchno) dispchno")->group("chno")->order('dispchno')->select();
        if($rsChno){
            foreach ($rsChno as $key => $value) {
                $rsChno[$key]['channelname'] = $this->getIDByName($value['dispchno']);
            }
        }
        $userbrowser = $_SERVER['HTTP_USER_AGENT'];



        $this->assign("rsChno",$rsChno);
        $this->assign("datetimepicker_start",$s_time);
        $this->assign("datetimepicker_end",$e_time);
        $this->assign("CurrentPage",'recordList');

        if ( preg_match( '/MSIE/i', $userbrowser )  || preg_match('/Trident/i', $userbrowser) ) {
            $this->display("thirdRecordList_ie");
        } else {
            $this->display();
        }
    }

    //账号查询
    public function searchRecordList(){
        $s_time = date("Y-m-d",time())." 00:00:00";
        $e_time = date("Y-m-d",time())." 23:59:59";

        if(IS_AJAX){
            $start = isset($_REQUEST['start'])?$_REQUEST['start']:0;
            $length = isset($_REQUEST['length'])?$_REQUEST['length']:10;
            $SearchMode = isset($_REQUEST['SearchMode'])?$_REQUEST['SearchMode']:0;
            $search_key = $_REQUEST['search_key'];
            $has_video = $_REQUEST['has_video'];
            $stime = isset($_REQUEST['stime'])?$_REQUEST['stime']:1;
            $stime = empty($stime)?0:$stime;
            $InOut = $_REQUEST['InOut'];
            $chk_CN = $_REQUEST['chk_CN'];
            $datetimepicker_start = isset($_REQUEST['datetimepicker_start'])?$_REQUEST['datetimepicker_start']:$s_time;
            $datetimepicker_end = isset($_REQUEST['datetimepicker_end'])?$_REQUEST['datetimepicker_end']:$e_time;
            //$name = $_REQUEST['search']['value'];
            $m = M("rec_cdrinfo");
            $m_bak = M("rec_bakinfo");

            $sEcho = $_REQUEST['sEcho']; // DataTables 用来生成的信息
            $output['sEcho'] = $sEcho;
            $field="N_SN,N_ChannelID,D_StartTime,D_StopTime,V_VoiceFile,N_FileFormat,";
            $field.="N_CallDirection,V_Caller,V_Called,V_Ext,N_BackUp,N_Lock,N_Alarm,";
            $field.="N_AvoidRec,N_DeleteFlag,N_Locker,V_Path,V_NetPath,";
            $field.="V_Diverter,V_Diverted,N_IsTalk,ReMark ";


            $where  .=  " D_StartTime between '{$datetimepicker_start}' and '$datetimepicker_end' ";

            if(trim($chk_CN) !='' && $chk_CN != 'null'){
                $where  .= " and N_ChannelID in ($chk_CN)";
            }

            if($search_key != ""){//通话号码(姓名)的取值，0则号码，1则姓名
                if($SearchMode ==1){
                    $numArr=getNumByName($search_key);
                    if(!$numArr || empty($numArr)){
                        $where .= ' and N_SN = -1';//姓名没匹配到号码，直接设置一个不成立的条件
                    }else{
                        $where  .= " and (";
                        foreach($numArr as $k=>$v){
                            if($k == 0){
                                $where  .= "   v_caller  = '{$v}' or v_called  = '{$v}' or v_ext = '{$v}' ";
                            }
                            else{
                                $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}'  or v_ext = '{$v}' ";
                            }

                        }
                        $where  .= ")";
                    }
                }else{
                    $where .= " and (V_caller like '".$search_key."%' or V_called like '".$search_key."%'  or v_ext like '".$search_key."%')";
                }
                //echo $where;die;
            }
            if ($has_video==1) {//只查有录象的记录
                $sql = "(select `recid` from tab_ved_cdrinfo) union (select `recid` from tab_ved_bakinfo) order by `recid` ";
                $res = M()->query($sql);
                $recid = "";
                if($res){
                    foreach ($res as $v){
                        if($recid == ""){
                            $recid = $v['recid'];
                        }
                        else
                        {
                            $recid = $recid .",".$v['recid'];
                        }
                    }
                }
                if(!empty($recid)){
                   $where .= " and N_SN in ($recid)";
                }
                else{
                   $where .= " and N_SN = -1 ";
                }
            }
            //echo $where;
            //按拨叫方向查询
            if($InOut != 2){
                $where .= " and N_CallDirection = '{$InOut}' ";
            }
            $where.=" and  (unix_timestamp(D_StopTime)-unix_timestamp(D_StartTime))>=$stime ";
            $CntNoBackup=$m->field("count(*) cnt")->where($where)->find();  //未备份部分数据
            $CntNoBackup=$CntNoBackup['cnt'];
            $CntBackup = $m_bak->field("count(*) cnt")->where($where)->find();  //已备份部分数据
            $CntBackup=$CntBackup['cnt'];
            $total = (int)$CntNoBackup + (int)$CntBackup;
            $rs = array();
            if($CntBackup==0)
            {
                //无已备份数据，读取未备份数据即可
                $rs= $m->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
            }
            else
            {
                if($CntNoBackup==0)
                {
                    //有已备份数据，无未备份数据。读取已备份份数据即可
                    $rs= $m_bak->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
                }
                else
                {
                    //未备份和已备份都有，需要读取两张表
                    //计算未备份数据有多少页$NotBak_pagecnt
                    if($CntNoBackup % $length==0){
                        $NotBak_pagecnt=($CntNoBackup / $length);
                    }else{
                        $NotBak_pagecnt=(int)($CntNoBackup/$length)+1;
                    }
                    $NumNotShowed = $CntNoBackup - $start;  //未备份数据还有多少条未显示

                    if($NumNotShowed>0){
                        if($NumNotShowed >$length || $NumNotShowed == $length)
                        {
                            $rs= $m->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
                        }
                        else
                        {
                            //未备份数据不足一页的数据，需要同时读取备份与未备份数据
                            $rsNoBak= $m->field($field)->where($where)->limit($start,$length)->order("D_StartTime desc")->select();
                            $rsBak= $m_bak->field($field)->where($where)->limit(0,($length-$NumNotShowed))->order("D_StartTime desc")->select();
                            $rs= array_merge($rsNoBak,$rsBak);
                        }
                   }
                   else
                   {
                       //$NumNotShowed=0或者$NumNotShowed<0,则未备份录音已经显示完毕，仅查询备份表(tab_rec_bakinfo)即可
                       $rs= $m_bak->field($field)->where($where)->limit($start-$CntNoBackup,$length)->order("D_StartTime desc")->select();
                   }
                }
            }

            if($rs){
                foreach ($rs as $k=>$v)
                {
                    $fileplay ="<i class='fa fa-volume-up  fa-2x' style=\"cursor:pointer\" onclick=\"play('".$v["n_sn"]."','".$v["v_voicefile"]."')\"></i>";

                    $rs[$k]['n_channelinfo'] = getChannelNameById($v['n_channelid']);
                    $rs[$k]['v_caller'] = getNameByPhoneNum($v['v_caller']);
                    $rs[$k]['v_called'] = getNameByPhoneNum($v['v_called']);
                    $rs[$k]['v_ext'] = getNameByPhoneNum($v['v_ext']);
                    $rs[$k]['n_calldirection'] = $v['n_calldirection']==1?'拨出':'来电';
                    $rs[$k]['longtime'] = DateDiff($v['d_starttime'],$v['d_stoptime']);
                    $rs[$k]['v_voicefileplay'] = $fileplay;
                    $rs[$k]['local_video'] = getVideoByRecID($v['n_sn'],0);
                    $rs[$k]['remote_video'] = getVideoByRecID($v['n_sn'],1);
                    //$rs[$k]['translate'] = get_translate($v['n_sn']);
                    $str = "";
                    if($v["n_lock"] == 1){
                        $str.="<i class='fa fa-lock fa-2x' alt='已锁定'";
                    }
                    else{
                        $str.="<i class='fa fa-unlock fa-2x' alt='未锁定'";
                    }

                    if(canLock()==1){
                        $str.="   style='cursor:pointer;'";
                        $str.=" onclick='lock(".$v["n_sn"].",".$v["n_lock"].",".$v["n_backup"].",this);'";
                    }else{
                        $str.=($v["n_lock"]==1)?" alt='已锁定'":" alt='未锁定'";
                    }
                    $str.=" id='img_lock_".$v["n_sn"]."'";
                    $str.=" /></i>";
                    $rs[$k]['n_lock_info'] = $str;

                    $str = "";
                    if($v["remark"]=="" || $v["remark"]==NULL){
                        $icoRemark="<img id='ico_".$v["n_sn"]."' src='/Public/assets/images/addRemark.gif' />";
                    }else{
                        $icoRemark="<img id='ico_".$v["n_sn"]."' src='/Public/assets/images/Remark.gif' />";
                    }
                    $str.="<span align='center' data-rm='".$v["remark"]."' onclick=\"Remark('".$v["n_sn"]."','".$v["n_backup"]."',this)\" style='cursor:pointer' ";
                    $str.=" title=\"".$v["remark"]."\" >{$icoRemark}</span>";
                    $rs[$k]['remark_info'] = $str;
                }
            }


            $output['aaData'] = $rs;
            $output['iTotalDisplayRecords'] = $total;    //如果有全局搜索，搜索出来的个数
            $output['iTotalRecords'] = $total; //总共有几条数据
            echo json_encode($output); //最后把数据以json格式返回
            die;
        }

        //列出所有通道
        $rsChno = M('drcu_circfg')->order('dispchno')->field("distinct chno")->select();
        if($rsChno){
            foreach ($rsChno as $key => $value) {
                 $rsChno[$key]['channelname'] = $this->getIDByName($value['chno']);
            }
        }
        $this->assign("rsChno",$rsChno);

        $this->assign("datetimepicker_start",$s_time);
        $this->assign("datetimepicker_end",$e_time);
        $this->assign("CurrentPage",'searchRecordList');
        $this->display();
    }


   /*
        锁定录音/录音解锁
        锁定状态的录音禁止删除
        用户点击录音信息查询页面的锁定按钮时，讲对应的
     */
   public function recordLockop(){

        $isBack=$_REQUEST["n_backup"];
        $isLock=$_REQUEST["n_lock"];
        $N_SN=$_REQUEST["n_sn"];
        $tbname=($isBack==1)?"rec_bakinfo":"rec_cdrinfo";
        $data = array();
        $data['N_Lock'] = $isLock;
        $rs = M($tbname)->where("N_SN={$N_SN}")->save($data);
        $msg = $isLock==1?'锁定':'解锁';
        $AppResult = new AppResult;
        if(false !== $rs){
            $AppResult->code = 1;
            $AppResult->data = '';
            $AppResult->message = $msg.'成功';
        }
        else
        {
            $AppResult->code = 0;
            $AppResult->data = '';
            $AppResult->message =$msg.'失败';
        }
        $AppResult->returnJSON();

    }

    /*
           录音备注编辑

        */
    public function remarkedit(){

        $remark=MySQLFixup(isset($_POST["remark"])?$_POST["remark"]:"");
        $isBack = $_REQUEST['n_backup'];
        $sn = $_REQUEST['n_sn'];
        $tbname=($isBack==1)?"rec_bakinfo":"rec_cdrinfo";
        $save['ReMark'] = $remark;
        $rs = M($tbname)->where("N_SN ='{$sn}'")->save($save);
        //echo M()->getLastSql();
        $AppResult = new AppResult;
        if(false !== $rs){
            $AppResult->code = 1;
            $AppResult->data = '';
            $AppResult->message ='成功编辑备注信息';
        }
        else
        {
            $AppResult->code = 0;
            $AppResult->data = '';
            $AppResult->message ='失败';
        }
        $AppResult->returnJSON();
    }

    public function getIDByName($id){
        $rs = M("sys_paramschannel")->field("V_Value")->where("N_ChannelNo='{$id}' and V_ParamsName ='ChnName'")->find();
        if($rs){
            $str = empty($rs["v_value"])?$id:$rs["v_value"];
        }
        else{
            $str = $id;
        }
        return $str;

    }

    public function recordCount(){
        $s_time = date("Y-m-d",strtotime("-7 day"))." 00:00:00";
        $e_time = date("Y-m-d",strtotime("-1 day"))." 23:59:59";
        $datetimepicker_start = isset($_REQUEST['datetimepicker_start'])?$_REQUEST['datetimepicker_start']:$s_time;
        $datetimepicker_end = isset($_REQUEST['datetimepicker_end'])?$_REQUEST['datetimepicker_end']:$e_time;
        $SearchMode = isset($_REQUEST['SearchMode'])?$_REQUEST['SearchMode']:1;
        $orgCaller=isset($_POST["orgCaller"])?$_POST["orgCaller"]:'';
        $orgCallee=isset($_POST["orgCallee"])?$_POST["orgCallee"]:'';
        $v_caller=isset($_POST["v_caller"])?$_POST["v_caller"]:'';
        $v_called=isset($_POST["v_called"])?$_POST["v_called"]:'';
        $optype=isset($_POST["optype"])?$_POST["optype"]:'';



        $where  = " D_StartTime >= '{$datetimepicker_start}' and D_StartTime <= '{$datetimepicker_end}' ";
        $where  .= " and D_StopTime != '0000-00-00 00:00:00' ";
        if($v_caller)  $where  .= " and v_caller  = '{$v_caller}' ";
        if($v_called)  $where  .= " and v_called  = '{$v_called}' ";
        if(IS_AJAX){
            $sEcho = $_REQUEST['sEcho']; // DataTables 用来生成的信息
            $output['sEcho'] = $sEcho;
            $start = isset($_REQUEST['start'])?$_REQUEST['start']:0;
            $length = isset($_REQUEST['length'])?$_REQUEST['length']:10;
            if($optype == 'excel'){
                header("Content-type: text/html; charset=utf-8");
                Vendor("PHPExcel.PHPExcel");
                Vendor("PHPExcel.PHPExcel.Reader.Excel2007.php");
                Vendor("PHPExcel.PHPExcel.Reader.Excel5");
                Vendor("PHPExcel.PHPExcel.IOFactory");
                $excel = new \PHPExcel();
            }

            if($SearchMode == 1)
            {
                $title ='录音统计记录01';
                $field = "N_ChannelID,Count(N_SN) as 'cAmount',Sum(ABS(TIMESTAMPDIFF(SECOND,D_StartTime,D_StopTime))) as cSecond";
                if($optype == 'excel'){
                    //指定工作簿
                    $excel->setActiveSheetIndex(0);
                    //设置列宽
                    $excel->getActiveSheet()->getColumnDimension('A')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('B')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('C')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('D')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('F')->setWidth('20');

                    $excel->getActiveSheet()->setTitle('录音统计'); //excel标题
                    //设置行标题
                    $excel->getActiveSheet()->setCellValue("A1", "通道号");
                    $excel->getActiveSheet()->setCellValue("B1", "通道名称");
                    $excel->getActiveSheet()->setCellValue("C1", "录音次数");
                    $excel->getActiveSheet()->setCellValue("D1", "录音时长");
                    $excel->getActiveSheet()->setCellValue("E1", "录像次数");
                    $excel->getActiveSheet()->setCellValue("F1", "录像时长");
                    $rs = M('rec_bakinfo')->field($field)->where($where)->group('N_ChannelID')->select();
                    //echo M()->getLastSql();
                    if($rs){
                        $j = 2;
                        foreach ($rs as $k=>$v){
                            $idres = getCircfgByid($v['n_channelid']);
							$rs[$k]['n_channelid'] = isset($idres['chno'])?$idres['chno']:0;
                            $data = $this->videoSimpleCount($rs[$k]['n_channelid'],$where);
                            $excel->getActiveSheet()->setCellValue("A".$j,$rs[$k]['n_channelid']);
                            $excel->getActiveSheet()->setCellValue("B".$j,getChannelNameById($rs[$k]['n_channelid']));
                            $excel->getActiveSheet()->setCellValue("C".$j,$v['camount']);
                            $excel->getActiveSheet()->setCellValue("D".$j,formatTime($v["csecond"]));
                            $excel->getActiveSheet()->setCellValue("E".$j,$data['vtimes']);
                            $excel->getActiveSheet()->setCellValue("F".$j,$data['vsecond']);
                            $j++;
                        }
                    }
                }
                else{
                    $cnt = M('rec_bakinfo')->field("N_ChannelID ")->where($where)->group('N_ChannelID')->select();
                    $total = count($cnt);

                    $rs = M('rec_bakinfo')->field($field)->where($where)->group('N_ChannelID')->limit($start,$length)->select();
                    //echo M()->getLastSql();
                    if($rs){
                        foreach ($rs as $k=>$v){
                            $idres = getCircfgByid($v['n_channelid']);
							$rs[$k]['n_channelid'] = isset($idres['chno'])?$idres['chno']:0;
                            $rs[$k]['channelname'] = getChannelNameById($rs[$k]['n_channelid']);
                            $rs[$k]['recordtime'] = formatTime($v["csecond"]);
                            $data = $this->videoSimpleCount($rs[$k]['n_channelid'],$where);
                            $rs[$k]['vtimes'] = $data['vtimes'];
                            $rs[$k]['vsecond'] = $data['vsecond'];
                        }
                    }
                }

            }
            else
            {
                $title ='录音统计记录02';
                $field = "*";
                if($optype == 'excel'){
                    //指定工作簿
                    $excel->setActiveSheetIndex(0);
                    //设置列宽
                    $excel->getActiveSheet()->getColumnDimension('A')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('B')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('C')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('D')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('F')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('G')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('H')->setWidth('20');
                    $excel->getActiveSheet()->getColumnDimension('I')->setWidth('20');

                    $excel->getActiveSheet()->setTitle('录音统计'); //excel标题
                    //设置行标题
                    $excel->getActiveSheet()->setCellValue("A1", "通道号");
                    $excel->getActiveSheet()->setCellValue("B1", "通道名称");
                    $excel->getActiveSheet()->setCellValue("C1", "主叫");
                    $excel->getActiveSheet()->setCellValue("D1", "被叫");
                    $excel->getActiveSheet()->setCellValue("E1", "录音号");
                    $excel->getActiveSheet()->setCellValue("F1", "放向");
                    $excel->getActiveSheet()->setCellValue("G1", "录音/录像");
                    $excel->getActiveSheet()->setCellValue("H1", "开始时间");
                    $excel->getActiveSheet()->setCellValue("I1", "时长");

                    $rs = M('rec_bakinfo')->field($field)->where($where)->select();
                    //echo M()->getLastSql();
                    if($rs){
                        $j = 2;
                        foreach ($rs as $k=>$v){
							$idres = getCircfgByid($v['n_channelid']);
							$rs[$k]['n_channelid'] = isset($idres['chno'])?$idres['chno']:0;
                            $channelname = getChannelNameById($rs[$k]['n_channelid']);
                            $longtime = DateDiff($v['d_starttime'],$v['d_stoptime']);
                            $n_calldirection = $v['n_calldirection']==1?'拨出':'来电';
                            $v_caller = getNameByPhoneNum($v['v_caller']);
                            $v_called = getNameByPhoneNum($v['v_called']);

                            $excel->getActiveSheet()->setCellValue("A".$j,$rs[$k]['n_channelid']);
                            $excel->getActiveSheet()->setCellValue("B".$j,$channelname);
                            $excel->getActiveSheet()->setCellValue("C".$j,$v_caller);
                            $excel->getActiveSheet()->setCellValue("D".$j,$v_called);
                            $excel->getActiveSheet()->setCellValue("E".$j,$v_caller);
                            $excel->getActiveSheet()->setCellValue("F".$j,$n_calldirection);
                            $excel->getActiveSheet()->setCellValue("G".$j,$v['v_voicefile']);
                            $excel->getActiveSheet()->setCellValue("H".$j,$v['d_starttime']);
                            $excel->getActiveSheet()->setCellValue("I".$j,$longtime);
                            $j++;
                        }
                    }
                }
                else{
                    $cnt = M('rec_bakinfo')->field("count(*) cnt ")->where($where)->find();
                    $total = $cnt['cnt'];
                    $rs = M('rec_bakinfo')->field($field)->where($where)->limit($start,$length)->select();
                    //echo M()->getLastSql();exit;
                    if($rs){
                        foreach ($rs as $k=>$v){
							$idres = getCircfgByid($v['n_channelid']);
							$rs[$k]['n_channelid'] = isset($idres['chno'])?$idres['chno']:0;
                            $rs[$k]['channelname'] = getChannelNameById($rs[$k]['n_channelid']);
                            $rs[$k]['longtime'] = DateDiff($v['d_starttime'],$v['d_stoptime']);
                            $rs[$k]['n_calldirection'] = $v['n_calldirection']==1?'拨出':'来电';
                            $rs[$k]['v_caller'] = getNameByPhoneNum($v['v_caller']);
                            $rs[$k]['v_called'] = getNameByPhoneNum($v['v_called']);
                            $rs[$k]['v_ext'] = getNameByPhoneNum($v['v_ext']);
                        }
                    }
                }
            }
            if($optype == 'excel' && IS_AJAX){
                $date = date('Y-m-d',time());
                $fileurl = "Uploads/records/". $title."_".$date.".xls";
                mkdir_for_url($fileurl);
                $filename = iconv('utf-8', "gb2312", $fileurl);
                $objwriter = \PHPExcel_IOFactory::createWriter($excel,'Excel5');
                $objwriter->save($filename);
                $this->returnJSON(1,'导出成功',"/".$fileurl);
            }
            $output['aaData'] = $rs;
            $output['iTotalDisplayRecords'] = $total;    //如果有全局搜索，搜索出来的个数
            $output['iTotalRecords'] = $total; //总共有几条数据
            echo json_encode($output); //最后把数据以json格式返回
            die;
        }




        $this->assign("orgCaller",$orgCaller);
        $this->assign("orgCallee",$orgCallee);
        $this->assign("v_caller",$v_caller);
        $this->assign("v_called",$v_called);
        $this->assign("SearchMode",$SearchMode);
        $this->assign("datetimepicker_start",$datetimepicker_start);
        $this->assign("datetimepicker_end",$datetimepicker_end);
        $this->assign("CurrentPage",'recordCount');
        $this->display();
    }

    public function recordChart(){
        $s_time = date("Y-m-d", strtotime("-7 day"))." 00:00:00";
        $e_time = date("Y-m-d",strtotime("-1 day"))." 23:59:59";
        $SearchMode1=isset($_POST["SearchMode1"])?$_POST["SearchMode1"]:'';
        $SearchMode2=isset($_POST["SearchMode2"])?$_POST["SearchMode2"]:'';
        $search_key=isset($_POST["search_key"])?$_POST["search_key"]:'';
        $datetimepicker_start = isset($_REQUEST['datetimepicker_start'])?$_REQUEST['datetimepicker_start']:$s_time;
        $datetimepicker_end = isset($_REQUEST['datetimepicker_end'])?$_REQUEST['datetimepicker_end']:$e_time;


        $where  = "  D_StopTime != '0000-00-00 00:00:00' ";
        if(trim($SearchMode1) != '' && trim($SearchMode2) != ''){
            if($search_key !=''){
                $where  .= " and ((v_caller  = '{$search_key}' and N_CallDirection = 1) or  (v_called  = '{$search_key}'  and N_CallDirection = 0)) ";
            }
        }
        else{

            if(trim($SearchMode1) != ''){
                if($search_key !=''){
                    $where  .= " and v_caller  = '{$search_key}' ";
                }
                $where  .= " and N_CallDirection = 1 ";
            }

            if(trim($SearchMode2) != ''){
                if($search_key !=''){
                    $where  .= " and v_called  = '{$search_key}' ";
                }
                $where  .= " and N_CallDirection = 0 ";
            }
        }
        if(trim($SearchMode1) == '' && trim($SearchMode2) == ''){
            if($search_key !=''){
                $where  .= " and ((v_caller  = '{$search_key}' and N_CallDirection = 1) or  (v_called  = '{$search_key}'  and N_CallDirection = 0))  ";
            }
        }
        //按时间区分
        if($datetimepicker_start > $datetimepicker_end )
        {
            $datetimepicker_start = $s_time;
            $datetimepicker_end = $e_time;
            echo "<script>alert('开始时间不能大于结束时间')</script>";
        }
        $sdate=strtotime($datetimepicker_start);
        $edate=strtotime($datetimepicker_end);
        $dif=ceil(($edate-$sdate)/86400);

        $ddt = array();
        $huchu = array();
        $huru = array();
        $cSecond = array();
        $tabledata = array();
        if($dif){
            if(IS_AJAX){
                header("Content-type: text/html; charset=utf-8");
                Vendor("PHPExcel.PHPExcel");
                Vendor("PHPExcel.PHPExcel.Reader.Excel2007.php");
                Vendor("PHPExcel.PHPExcel.Reader.Excel5");
                Vendor("PHPExcel.PHPExcel.IOFactory");
                $excel = new \PHPExcel();

                //指定工作簿
                $excel->setActiveSheetIndex(0);
                //设置列宽
                $excel->getActiveSheet()->getColumnDimension('A')->setWidth('20');
                $excel->getActiveSheet()->getColumnDimension('B')->setWidth('20');
                $excel->getActiveSheet()->getColumnDimension('C')->setWidth('20');
                $excel->getActiveSheet()->getColumnDimension('D')->setWidth('20');

                $excel->getActiveSheet()->setTitle('录音记录'); //excel标题
                //设置行标题
                $excel->getActiveSheet()->setCellValue("A1", "日期");
                $excel->getActiveSheet()->setCellValue("B1", "呼入量");
                $excel->getActiveSheet()->setCellValue("C1", "呼出量");
                $excel->getActiveSheet()->setCellValue("D1", "通话时长");
            }
            $k = 1;
            for($i=0;$i < $dif;$i++){
                $dd = date("Y-m-d",strtotime("+{$i} day",strtotime($datetimepicker_start)));
                $where2  =$where . " and D_StartTime >= '{$dd} 00:00:00' and D_StartTime <= '{$dd} 23:59:59'";
                $field = "N_ChannelID,sum(case  N_CallDirection when 1 then 1 else 0 end ) huchu,sum(case  N_CallDirection when 0 then 1 else 0 end ) huru,Sum(ABS(TIMESTAMPDIFF(SECOND,D_StartTime,D_StopTime))) as cSecond";
                $rs = M('rec_bakinfo')->field($field)->where($where2)->find();
                $ddt[] = $dd;
                $huchu[] = isset($rs['huchu'])&& $rs['huchu']?(int)$rs['huchu']:0;
                $huru[] = isset($rs['huru'])&& $rs['huru']?(int)$rs['huru']:0;
                $cSecond[] = isset($rs['csecond']) && $rs['csecond']?(int)$rs['csecond']:0;
                $tabledata[$i]['dt'] = $dd;
                $tabledata[$i]['huchu'] = isset($rs['huchu'])&& $rs['huchu']?(int)$rs['huchu']:0;
                $tabledata[$i]['huru'] = isset($rs['huru'])&& $rs['huru']?(int)$rs['huru']:0;
                $csecond = isset($rs['csecond']) && $rs['csecond'] ?(int)$rs['csecond']/60:0;
                $csecond = round($csecond,2);
                $tabledata[$i]['csecond'] = floatval($csecond);

                if(IS_AJAX){
                    $k++;
                    $excel->getActiveSheet()->setCellValue("A".$k,$dd);
                    $excel->getActiveSheet()->setCellValue("B".$k,$tabledata[$i]['huchu']);
                    $excel->getActiveSheet()->setCellValue("C".$k,$tabledata[$i]['huru']);
                    $excel->getActiveSheet()->setCellValue("D".$k,$tabledata[$i]['csecond']);
                }
            }

            if(IS_AJAX){
                $title ='录音统计记录03';
                $date = date('Y-m-d',time());
                $fileurl = "Uploads/records/". $title."_".$date.".xls";
                $filename = iconv('utf-8', "gb2312", $fileurl);

                $objwriter = \PHPExcel_IOFactory::createWriter($excel,'Excel5');
                $objwriter->save($filename);
                $this->returnJSON(1,'导出成功',"/".$fileurl);
            }
        }

        $divWidth = $dif*128 ;
        $divWidth = $divWidth < 1280 ?1280:$divWidth;
        $p = round(100/($dif+1),2)."%";
        $this->assign("p",$p);

        $this->assign("divWidth",$divWidth);
        $this->assign("jsddt",json_encode($ddt));
        $this->assign("ddt",$ddt);
        $this->assign("jshuchu",json_encode($huchu));
        $this->assign("huchu",$huchu);
        $this->assign("jshuru",json_encode($huru));
        $this->assign("jscsecond",json_encode($cSecond));
        $this->assign("csecond",$cSecond);
        $this->assign("tabledata",$tabledata);
        $this->assign("search_key",$search_key);
        $this->assign("SearchMode1",$SearchMode1);
        $this->assign("SearchMode2",$SearchMode2);
        $this->assign("datetimepicker_start",$datetimepicker_start);
        $this->assign("datetimepicker_end",$datetimepicker_end);
        $this->assign("CurrentPage",'recordCount');
        $this->display();
    }

    public function diskManerage(){
        if(IS_AJAX){
            $sEcho = $_REQUEST['sEcho']; // DataTables 用来生成的信息
            $output['sEcho'] = $sEcho;

            $out['D_id'] = 1;
            $out['D_name'] = 'C:/';
            $out['D_used'] = '500';
            $out['D_total'] = '1000';
            $out['D_path'] = 'D:/files/';
            $out['D_space'] = '1024';

            for($i=1;$i<=5;$i++){
                $aaData[]=$out;
                $output['aaData'] = $aaData;
            }

            $output['iTotalDisplayRecords'] = 5;    //如果有全局搜索，搜索出来的个数
            $output['iTotalRecords'] = 5; //总共有几条数据
            echo json_encode($output); //最后把数据以json格式返回
            die;
        }
    }

    //查询录象的时长、次数
    public function videoSimpleCount($N_ChannelID,$where)
    {
        $sql = "(select `recid` from tab_ved_cdrinfo) union (select `recid` from tab_ved_bakinfo) order by `recid` ";
        $res = M()->query($sql);
        $recid = "";
        if($res){
            foreach ($res as $v){
                if($recid == ""){
                    $recid = $v['recid'];
                }
                else
                {
                    $recid = $recid .",".$v['recid'];
                }
            }
        }
        $where2 ="";
        if(!empty($recid)) $where2 .= " and N_SN in ($recid)";

        $sql="Select N_ChannelID,Count(N_SN) as 'cAmount',";
        $sql.="Sum(ABS(TIMESTAMPDIFF(SECOND,D_StartTime,D_StopTime))) as 'cSecond' from tab_rec_bakinfo where ".$where."
	    AND `N_ChannelID`='$N_ChannelID' {$where2}";
        $sql.=" Group by N_ChannelID ";
        $rowsSimpleVideo = M()->query($sql);
        $data = array();
        $data["vtimes"] = $rowsSimpleVideo["cAmount"]==""?'0':$rowsSimpleVideo["cAmount"];
        $data["vsecond"] = formatTime($rowsSimpleVideo["cSecond"]);
        return $data;
    }
    public function voicePlayer()
    {
        $listItem=$this->getParam("listItem");
        $fileName="";
        if($listItem==""){
            JS_alert("播放列表为空，请选择要播放的录象");
        }
        $SelectStr="N_SN,N_RECID,N_RECSEQ,N_ChannelID,D_StartTime,D_StopTime,V_Diverter,V_Diverted";
        $SelectStr.=",N_CallDirection,V_Caller,V_Called,V_Ext";
        $SelectStr.=",v_path,v_netpath,v_voicefile,(unix_timestamp(D_StopTime)-unix_timestamp(D_StartTime)) AS reclong ";

        $aryItem=explode(",",$listItem);
        $rsPlay = array();
        $rsPlay_bak = array();
        if(count($aryItem)>1){//试听多个录音
            $rs = M('rec_cdrinfo')->where("N_SN in($listItem)")->select();
            $cnt = count($rs);
            $rsPlay = $rs;

            $rs_bak = M('rec_bakinfo')->where("N_SN in($listItem)")->select();
            $cnt_bak = count($rs_bak);
            $rsPlay_bak = $rs_bak;

            if(($cnt+$cnt_bak)==0){
                JS_alert("没找到录象文件，可能已被删除");
                //JScript("self.close();");
            }
        }else{//试听一个录音，根据录音子ID判断是否有相关录音，有则罗列出来一起试听。
            $rs = M('rec_cdrinfo')->field("N_RECID")->where("N_SN = '{$listItem}' ")->find();

            $recid = "";
            $rsPlay = false;
            if(!empty($rs["n_recid"])){

                $recid = $rs["n_recid"];
                $rsPlay = M('rec_cdrinfo')->field($SelectStr)->where("N_RECID = '{$recid}' ")->order('N_RECSEQ')->select();
            }


            $fileName=$this->getParam("fname");
            //加载页面后，首先播放的录音
            if(!$rsPlay){

                $rs = M('rec_bakinfo')->field("N_RECID")->where("N_SN = '{$listItem}' ")->find();
                $rsPlay = false;
                $recid = "";
                if(!empty($rs["n_recid"])){
                    $recid = $rs["n_recid"];

                    $rsPlay = M('rec_bakinfo')->field($SelectStr)->where("N_RECID = '{$recid}' ")->order('N_RECSEQ')->select();
                }

                if(!$rsPlay){	//再次无记录，关闭试听页面
                    JS_alert("没找到录音文件，可能已被删除");
                    exit;
                    //JScript("self.close();");
                }
            }
        }
        $rsPlayTotal = array_merge($rsPlay,$rsPlay_bak);
        if($rsPlayTotal){
            foreach ($rsPlayTotal as $k=>$v){
                $rsPlayTotal[$k]['field_url'] = host().$v["v_netpath"].$v["v_voicefile"];
                $rsPlayTotal[$k]['fx'] =  fx($v["n_calldirection"]);
            }
        }


        $this->assign("rsPlayTotal",$rsPlayTotal);
        $this->assign("fileName",$fileName);
        $this->display();
    }
    //录音播放器（弃用，改用兼容chrome）
    public function recordPlayer()
    {
        $listItem=$this->getParam("listItem");
        $fileName="";
        if($listItem==""){
            JS_alert("播放列表为空，请选择要播放的录象");
        }
        $SelectStr="N_SN,N_RECID,N_RECSEQ,N_ChannelID,D_StartTime,D_StopTime,V_Diverter,V_Diverted";
        $SelectStr.=",N_CallDirection,V_Caller,V_Called,V_Ext";
        $SelectStr.=",v_path,v_netpath,v_voicefile,(unix_timestamp(D_StopTime)-unix_timestamp(D_StartTime)) AS reclong ";

        $aryItem=explode(",",$listItem);
        $rsPlay = array();
        $rsPlay_bak = array();
        if(count($aryItem)>1){//试听多个录音
            $rs = M('rec_cdrinfo')->where("N_SN in($listItem)")->select();
            $cnt = count($rs);
            $rsPlay = $rs;

            $rs_bak = M('rec_bakinfo')->where("N_SN in($listItem)")->select();
            $cnt_bak = count($rs_bak);
            $rsPlay_bak = $rs_bak;

            if(($cnt+$cnt_bak)==0){
                JS_alert("没找到录象文件，可能已被删除");
                //JScript("self.close();");
            }
        }else{//试听一个录音，根据录音子ID判断是否有相关录音，有则罗列出来一起试听。
            $rs = M('rec_cdrinfo')->field("N_RECID")->where("N_SN = '{$listItem}' ")->find();

            $recid = "";
            $rsPlay = false;
            if(!empty($rs["n_recid"])){

                $recid = $rs["n_recid"];
                $rsPlay = M('rec_cdrinfo')->field($SelectStr)->where("N_RECID = '{$recid}' ")->order('N_RECSEQ')->select();
            }


            $fileName=$this->getParam("fname");
            //加载页面后，首先播放的录音
            if(!$rsPlay){

                $rs = M('rec_bakinfo')->field("N_RECID")->where("N_SN = '{$listItem}' ")->find();
                $rsPlay = false;
                $recid = "";
                if(!empty($rs["n_recid"])){
                    $recid = $rs["n_recid"];

                    $rsPlay = M('rec_bakinfo')->field($SelectStr)->where("N_RECID = '{$recid}' ")->order('N_RECSEQ')->select();
                }

                if(!$rsPlay){	//再次无记录，关闭试听页面
                    JS_alert("没找到录音文件，可能已被删除");
                    exit;
                    //JScript("self.close();");
                }
            }
        }
        $rsPlayTotal = array_merge($rsPlay,$rsPlay_bak);
        if($rsPlayTotal){
            foreach ($rsPlayTotal as $k=>$v){
                $rsPlayTotal[$k]['field_url'] = host().$v["v_netpath"].$v["v_voicefile"];
                $rsPlayTotal[$k]['fx'] =  fx($v["n_calldirection"]);
            }
        }

        $this->assign("rsPlayTotal",$rsPlayTotal);
        $this->assign("fileName",$fileName);
        $this->display();
    }
    public function videoPlayerForIe(){
        $listItem=$this->getParam("listItem");
        //只保留有录象的选项
        $sqlVideo ="(select `RecID` from tab_ved_cdrinfo where `RecID` in($listItem)) union (select `RecID` from tab_ved_bakinfo where `RecID` in($listItem)) order by `RecID`";
        //echo $sqlVedio;
        $rsVideo=M()->query($sqlVideo);
        $listVideo="";
        foreach ($rsVideo as $v){
            if($listVideo == ""){
                $listVideo = $v['recid'];
            }
            else
            {
                $listVideo = $listVideo .",".$v['recid'];
            }
        }

        $listItem = $listVideo;
        //echo $listItem.'<br/>';

        $fileName="";
        if($listItem ==""){
            JS_alert("录像文件不存在，或者已被删除");
            echo "<script type='text/javascript'>window.close();</script>";
            exit();
        }

        $SelectStr="N_SN,N_RECID,N_RECSEQ,N_ChannelID,D_StartTime,D_StopTime,V_Diverter,V_Diverted";
        $SelectStr.=",N_CallDirection,V_Caller,V_Called,V_Ext,V_Path";
        $SelectStr.=",v_path,v_netpath,v_voicefile,(unix_timestamp(D_StopTime)-unix_timestamp(D_StartTime)) AS reclong ";

        $aryItem=explode(",",$listItem);
        $rsPlay = array();
        $rsPlay_bak = array();
        if(count($aryItem)>1){//试听多个录音
            $sql="select {$SelectStr} from tab_rec_cdrinfo where N_SN in($listItem)";
            $rsPlay=M()->query($sql);
            $sql="select {$SelectStr} from tab_rec_bakinfo where N_SN in($listItem)";
            $rsPlay_bak=M()->query($sql);
            $cnt=count($rsPlay);
            $cnt1=count($rsPlay_bak);
            if(($cnt+$cnt1)==0){
                JS_alert("没找到相关文件，可能已被删除");
                //JScript("self.close();");
            }
        }else{
            $sql="select {$SelectStr} from tab_rec_cdrinfo where N_SN=$listItem";
            //查找录象文件
            $sqlVideo="(select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_cdrinfo where `RecID`='$listItem') union (select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_bakinfo where `RecID`='$listItem') order by `recid`";//获取录象记录
            $rsVideo=M()->query($sqlVideo);
            $rowsVideo=$rsVideo[0];
            //查找录象文件
            $fileSelect=isset($_GET["fname"])?$_GET["fname"]:"";//加载页面后，首先播放的录音
            $fileName=($fileSelect==""?"":$rowsVideo[$fileSelect]);
            $rsPlay=M()->query($sql);
            if(count($rsPlay)==0){
                $rsPlay=M()->query(str_replace("tab_rec_cdrinfo","tab_rec_bakinfo",$sql));//录音信息表无相关信息时，查询备份表
                if(count($rsPlay)==0){	//再次无记录，关闭试听页面
                    JS_alert("没找到相关文件，可能已被删除");
                    //JScript("self.close();");
                }
            }
        }
        $rsPlayTotal = array_merge($rsPlay,$rsPlay_bak);
        $rows = array();
        if($rsPlayTotal){
            foreach ($rsPlayTotal as $k=>$v){
                $sqlVideo="(select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_cdrinfo where `RecID`='{$v['n_sn']}') union (select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_bakinfo where `RecID`='{$v['n_sn']}') order by `recid`";
                $rs = M()->query($sqlVideo);
                $VideoRs = $rs[0];
                //  本地按实际路径读取，远程访问按网络路径读取
                if (host()=='http://127.0.0.1:80/'||host()=='http://localhost/') {
                    $rows[$k]['localurl'] = $v['v_path'].$VideoRs["localfilename"];
                    $rows[$k]['remoteurl'] = $v['v_path'].$VideoRs["remotefilename"];
                }else{
                    $rows[$k]['localurl'] = host().$v["v_netpath"].$VideoRs["localfilename"];
                    $rows[$k]['remoteurl'] = host().$v["v_netpath"].$VideoRs["remotefilename"];
                }

                if($fileSelect == 'localfilename' && trim($VideoRs["localfilename"]) !='')
                {
                    $rows[$k]['localfilename'] = $VideoRs["localfilename"];
                    $rows[$k]['remotefilename'] = "";
                }
                else if($fileSelect == 'remotefilename' && trim($VideoRs["remotefilename"]) !='')
                {
                    $rows[$k]['localfilename'] = "";
                    $rows[$k]['remotefilename'] = $VideoRs["remotefilename"];
                }
                else{
                    $rows[$k]['localfilename'] = $VideoRs["localfilename"];
                    $rows[$k]['remotefilename'] = $VideoRs["remotefilename"];
                }

                $rows[$k]['n_channelid'] = $v["n_channelid"];
                $rows[$k]['v_caller'] = $v["v_caller"];
                $rows[$k]['v_called'] = $v["v_called"];
                $rows[$k]['fx'] =  fx($v["n_calldirection"]);
                $rows[$k]['d_starttime'] = $v["d_starttime"];
                $rows[$k]['v_ext'] = $v["v_ext"];
            }
        }
        else
        {
            JS_alert("没找到相关文件，可能已被删除");
        }

        $this->assign("rows",$rows);
        $this->assign("fileSelect",$fileSelect);
        $this->assign("fileName",$fileName);
        $this->display();
    }

    public function videoPlayer(){
        $listItem=$this->getParam("listItem");
        //只保留有录象的选项
        $sqlVideo ="(select `RecID` from tab_ved_cdrinfo where `RecID` in($listItem)) union (select `RecID` from tab_ved_bakinfo where `RecID` in($listItem)) order by `RecID`";
        //echo $sqlVedio;
        $rsVideo=M()->query($sqlVideo);
        $listVideo="";
        foreach ($rsVideo as $v){
            if($listVideo == ""){
                $listVideo = $v['recid'];
            }
            else
            {
                $listVideo = $listVideo .",".$v['recid'];
            }
        }

        $listItem = $listVideo;
        //echo $listItem.'<br/>';

        $fileName="";
        if($listItem ==""){
            JS_alert("录像文件不存在，或者已被删除");
            echo "<script type='text/javascript'>window.close();</script>";
            exit();
        }

        $SelectStr="N_SN,N_RECID,N_RECSEQ,N_ChannelID,D_StartTime,D_StopTime,V_Diverter,V_Diverted";
        $SelectStr.=",N_CallDirection,V_Caller,V_Called,V_Ext,V_Path";
        $SelectStr.=",v_path,v_netpath,v_voicefile,(unix_timestamp(D_StopTime)-unix_timestamp(D_StartTime)) AS reclong ";

        $aryItem=explode(",",$listItem);
        $rsPlay = array();
        $rsPlay_bak = array();
        if(count($aryItem)>1){//试听多个录音
            $sql="select {$SelectStr} from tab_rec_cdrinfo where N_SN in($listItem)";
            $rsPlay=M()->query($sql);
            $sql="select {$SelectStr} from tab_rec_bakinfo where N_SN in($listItem)";
            $rsPlay_bak=M()->query($sql);
            $cnt=count($rsPlay);
            $cnt1=count($rsPlay_bak);
            if(($cnt+$cnt1)==0){
                JS_alert("没找到相关文件，可能已被删除");
                //JScript("self.close();");
            }
        }else{
            $sql="select {$SelectStr} from tab_rec_cdrinfo where N_SN=$listItem";
            //查找录象文件
            $sqlVideo="(select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_cdrinfo where `RecID`='$listItem') union (select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_bakinfo where `RecID`='$listItem') order by `recid`";//获取录象记录
            $rsVideo=M()->query($sqlVideo);
            $rowsVideo=$rsVideo[0];
            //查找录象文件
            $fileSelect=isset($_GET["fname"])?$_GET["fname"]:"";//加载页面后，首先播放的录音
            $fileName=($fileSelect==""?"":$rowsVideo[$fileSelect]);
            $rsPlay=M()->query($sql);
            if(count($rsPlay)==0){
                $rsPlay=M()->query(str_replace("tab_rec_cdrinfo","tab_rec_bakinfo",$sql));//录音信息表无相关信息时，查询备份表
                if(count($rsPlay)==0){	//再次无记录，关闭试听页面
                    JS_alert("没找到相关文件，可能已被删除");
                    //JScript("self.close();");
                }
            }
        }
        $rsPlayTotal = array_merge($rsPlay,$rsPlay_bak);
        $rows = array();
        if($rsPlayTotal){
            foreach ($rsPlayTotal as $k=>$v){
                $sqlVideo="(select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_cdrinfo where `RecID`='{$v['n_sn']}') union (select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_bakinfo where `RecID`='{$v['n_sn']}') order by `recid`";
                $rs = M()->query($sqlVideo);
                $VideoRs = $rs[0];
                //  本地按实际路径读取，远程访问按网络路径读取
                if (host()=='http://127.0.0.1:80/'||host()=='http://localhost/') {
                    $rows[$k]['localurl'] = $v['v_path'].$VideoRs["localfilename"];
                    $rows[$k]['remoteurl'] = $v['v_path'].$VideoRs["remotefilename"];
                }else{
                    $rows[$k]['localurl'] = host().$v["v_netpath"].$VideoRs["localfilename"];
                    $rows[$k]['remoteurl'] = host().$v["v_netpath"].$VideoRs["remotefilename"];
                }

                if($fileSelect == 'localfilename' && trim($VideoRs["localfilename"]) !='')
                {
                    $rows[$k]['localfilename'] = $VideoRs["localfilename"];
                    $rows[$k]['remotefilename'] = "";
                }
                else if($fileSelect == 'remotefilename' && trim($VideoRs["remotefilename"]) !='')
                {
                    $rows[$k]['localfilename'] = "";
                    $rows[$k]['remotefilename'] = $VideoRs["remotefilename"];
                }
                else{
                    $rows[$k]['localfilename'] = $VideoRs["localfilename"];
                    $rows[$k]['remotefilename'] = $VideoRs["remotefilename"];
                }

                $rows[$k]['n_channelid'] = $v["n_channelid"];
                $rows[$k]['v_caller'] = $v["v_caller"];
                $rows[$k]['v_called'] = $v["v_called"];
                $rows[$k]['fx'] =  fx($v["n_calldirection"]);
                $rows[$k]['d_starttime'] = $v["d_starttime"];
                $rows[$k]['v_ext'] = $v["v_ext"];
            }
        }
        else
        {
            JS_alert("没找到相关文件，可能已被删除");
        }

        $this->assign("rows",$rows);
        $this->assign("fileSelect",$fileSelect);
        $this->assign("fileName",$fileName);
        $this->display();
    }


    //删除录音记录
    public function delRecord(){
        if(IS_POST){
            $AppResult = new AppResult();
            if( !canManage()){
                $AppResult->code = 1;
                $AppResult->data ="";
                $AppResult->message = '没有删除权限，请联系管理员';
                $AppResult->returnJSON();
            }
            //根据选中的ID，筛选非锁定录音并获取相关路径

            $strID = $this->getParam("strID");
            if($strID==""){
                $AppResult->code = 0;
                $AppResult->data ="";
                $AppResult->msg = "未选中要删除的记录";
                $AppResult->returnJSON();
            }

            $msg=$this->del("tab_rec_cdrinfo",$strID);
            $msg1=$this->del("tab_rec_bakinfo",$strID);

            if($msg ==3 && $msg1 ==3){
                $msg= "未能删除记录（锁定状态）";
            }
            else
            {
                if($msg ==1 || $msg1 == 1){
                    $msg =  "成功删除记录";
                }
                elseif($msg ==2 || $msg1 == 2){
                    $msg = "成功删除记录(锁定状态的记录未能删除)";
                }
                else{
                    $msg = "未能删除记录：所选记录为锁定状态";
                }
            }


            $AppResult->code = 1;
            $AppResult->data ="";
            $AppResult->message = $msg;
            $AppResult->returnJSON();

        }
    }

    //删除录音记录
    //$tab	----操作表
    //$strID ---要删除的ID字符串
    function del($tab,$strID){
        //根据ID字符串筛选出未锁定记录。
        $AppResult = new AppResult();

        $sql="select n_sn,v_path,v_voicefile from {$tab} where n_sn in(".$strID.") and N_Lock !=1;";//筛选出未锁定记录

        $rs=M()->query($sql);
        if($rs){
            foreach ($rs as $v){
                $sql="delete from {$tab} where n_sn='{$v['n_sn']}'";
                $del=M()->execute($sql);
                $path=$v["v_path"].$v["v_voicefile"];//取得文件路径

                if(false !== $del){
                    $delid[]=$v["n_sn"];//记录被删除的ID

                    if(file_exists($path)){
                        //成功删除记录，则删除相关文件(如果文件存在的话)
                        //录音记录判断，如果有其他录音记录共享此文件，则此文件不删除
                        $whereRecShare=" where 1=1";
                        $whereRecShare.=" and UPPER(V_VoiceFile) =UPPER('{$v['v_voicefile']}') ";
                        $sqlRecShare="(select n_sn from tab_rec_cdrinfo $whereRecShare limit 0,1) union (select n_sn from tab_rec_bakinfo $whereRecShare limit 0,1);";//

                        $rsRecShare=M()->query($sqlRecShare);
                        if (count($rsRecShare)>0) {
                            $log="成功删除录音记录(ID=".$v["n_sn"].")；因为文件".MySQLFixup($path)."至少还在录音记录(ID=".$rsRecShare["n_sn"].")使用，所以不删除";
                        }//录音记录判断，如果有其他录音记录共享此文件，则此文件不删除
                        else
                        {
                            if(@unlink($path)){	//尝试删除文件
                                $log="成功删除录音记录(ID=".$v["n_sn"].")：".MySQLFixup($path);
                            }else{
                                $log="成功删除录音(ID=".$v["n_sn"].")。相关文件(".MySQLFixup($path).")删除失败";
                            }
                        }
                        addSysLog($log);
                    }

                }else{
                    $log="成功删除录音(ID=".$v["n_sn"].")。相关文件(".MySQLFixup($path).")不存在，可能已被删除。";
                    addSysLog($log);
                }
                //删除录象记录
                $flagVediodel=0;	//录象记录删除标志 1为已删除记录，0为无记录
                $sqlVediorec="(select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_cdrinfo where `RecID`='{$v['n_sn']}') union (select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_bakinfo where `RecID`='{$v['n_sn']}') order by `recid`";//获取录象记录
                //echo $sqlVediorec;
                $rowsVediorecrs=M()->query($sqlVediorec);
                $rowsVediorec = $rowsVediorecrs[0];
                //var_dump($rowsVediorec);
                $vedioTable=array('tab_ved_cdrinfo','tab_ved_bakinfo');
                for ($i=0;$i<2;$i++){
                    $sqlVediodel="delete from {$vedioTable[$i]} where `RecID`=".$v["n_sn"];
                    //echo $sqlVediodel;
                    $del = M()->execute($sqlVediodel);
                    if($del){//判断是否有删除的记录
                        $flagVediodel=1;
                        break;
                    }
                }
                if($flagVediodel==1){
                    $delid[]=$v["n_sn"];//记录被删除的ID
                    $pathLocal=$v["v_path"].$rowsVediorec["localfilename"];//取得主叫录象路径
                    $pathRemote=$v["v_path"].$rowsVediorec["remotefilename"];//取得被叫录象路径
                    if(file_exists($pathLocal)||file_exists($pathRemote)){	//成功删除记录，则删除相关文件(如果文件存在的话)
                        if (file_exists($pathLocal)) {
                            //录象记录判断，如果有其他录象记录共享此文件，则此文件不删除
                            $whereVedShare=" where 1=1";
                            $whereVedShare.=" and (LocalFileName ='{$rowsVediorec["localfilename"]}' or RemoteFileName ='{$rowsVediorec["localfilename"]}') ";
                            $sqlVedShare="(select RecID from tab_ved_cdrinfo $whereVedShare limit 0,1) union (select RecID from tab_ved_bakinfo $whereVedShare limit 0,1);";//
                            $rsVedShare=M()->query($sqlVedShare);
                            if (count($rsVedShare)>0) {
                                $log="成功删除录象记录(RecID=".$v["n_sn"].")：因为主叫录象文件".MySQLFixup($pathLocal)."至少还在录象记录(RecID=".$rsVedShare["recid"].")使用，所以不删除；";
                            }else//录象记录判断，如果有其他录象记录共享此文件，则此文件不删除
                            {
                                if(@unlink($pathLocal)){	//尝试删除文件
                                    $log="成功删除录象记录(RecID=".$v["n_sn"].")：主叫录象文件".MySQLFixup($pathLocal)."成功删除；";
                                }else{
                                    $log="成功删除录象(RecID=".$v["n_sn"].")。主叫录象文件(".MySQLFixup($pathLocal).")删除失败；";
                                }
                            }
                        }else {
                            $log="成功删除录象(RecID=".$v["RecID"].")。主叫录象文件(".MySQLFixup($pathLocal).")不存在，可能已被删除；";
                        }
                        if (file_exists($pathRemote)) {
                            //录象记录判断，如果有其他录象记录共享此文件，则此文件不删除
                            $whereVedShare=" where 1=1";
                            $whereVedShare.=" and (LocalFileName ='{$rowsVediorec["remotefilename"]}' or RemoteFileName ='{$rowsVediorec["remotefilename"]}') ";
                            $sqlVedShare="(select RecID from tab_ved_cdrinfo $whereVedShare limit 0,1) union (select RecID from tab_ved_bakinfo $whereVedShare limit 0,1);";//
                            $rsVedShare=M()->query($sqlVedShare);
                            if (count($rsVedShare)>0) {
                                $log.="因为被叫录象文件".MySQLFixup($pathRemote)."至少还在录象记录(RecID=".$rsVedShare["recid"].")使用，所以不删除；";
                            }else//录象记录判断，如果有其他录象记录共享此文件，则此文件不删除
                            {
                                if(@unlink($pathRemote)){	//尝试删除文件
                                    $log.=" 被叫录象文件(".MySQLFixup($pathRemote).")成功删除";
                                }else{
                                    $log.="被叫录象文件(".MySQLFixup($pathRemote).")删除失败";
                                }
                            }
                        }else {
                            $log.="被叫录象文件(".MySQLFixup($pathRemote).")不存在，可能已被删除。";
                        }
                    }else{	//
                        $log="成功删除录象(RecID=".$v["n_sn"].")。相关录象文件(".MySQLFixup($pathLocal)."和".MySQLFixup($pathRemote).")都不存在，可能已被删除。";
                    }
                    addSysLog($log);
                }
            }
        }

        //返回操作信息
        if(count($rs)>0){
            $delid=implode($delid,",");//将数组转换为字符串
            if(strlen($strID)==strlen($delid)){
                return 1; //"成功删除记录";
            }else{
                return 2; //"成功删除记录(锁定状态的记录未能删除)";
            }
        }else{//("未能删除记录：所选记录为锁定状态");

            return 3; // "未能删除记录（锁定状态）";
        }
    }


    //打包下载
    public function recordZip()
    {
        set_time_limit(0);
        $AppResult = new AppResult();
        $strID=$this->getParam("strID");
        $type=$this->getParam("type",1);
        //$where=" where N_SN in({$strID})";
        $idArr= explode(",",$strID);
        //--------------------------------
        $retMsg = "";
        if($strID!=""){
            if($type == 2){
                $retMsg = $this->CreateZipVideo($idArr);
            }else{
                $retMsg = $this->CreateZip($idArr);
            }

        }else{
            $retMsg = "未选择下载文件";
        }
        $AppResult->code = 1;
        $AppResult->data = "";
        $AppResult->message = $retMsg;
        $AppResult->returnJSON();
    }

    //视频打包
    public function CreateZipVideo($where){

        $pre = C('DB_PREFIX');
        $zip=new \ZipArchive();
        $zipName = date("Ymdihs")."_".$_SESSION["uName"].".zip";
        $csvName = date("Ymdihs")."_".$_SESSION["uName"].".csv";
        $filename = "./Uploads/".$zipName;
        $retMsg = "";
        if($zip->open($filename, \ZIPARCHIVE::CREATE)!==TRUE) {
            $retMsg .= "无法创建 {$filename}";
        }else{
            //注：需要在打包时同时生成CSV文件压缩在里面，所以需要的信息比较多。
            //需要调整csv文件以配合本地播放软件时，调整此字段($sel值)即可。
            //v_path,v_netpath,v_voicefile是必选字段
            $sel="n_sn,n_channelid,d_starttime,d_stoptime,v_voicefile,n_fileformat,n_calldirection,";
            $sel.="v_caller,v_called,v_ext,v_diverter,v_diverted,v_path,v_netpath,n_istalk,remark";
            $sql="select {$sel} from tab_rec_cdrinfo ".$where;

            $csv = array();
            $path = array();
            $rows=M()->query($sql);
            $msg="";//记录下载详细信息;
            $n=0;
            $lost=0;
            $fp=fopen("./Uploads/".$csvName,"w");
            if($rows){
                foreach ($rows as $k=>$v)
                {
                    $RecID = getVideoByRecID($v['n_sn'],$type = 2);
                    if($RecID){
                        $sqlVideo="(select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_cdrinfo where `RecID`='$RecID') union (select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_bakinfo where `RecID`='$RecID') order by `recid`";//获取录象记录

                        $rs = M()->query($sqlVideo);
                        $VideoRs = $rs[0];
                        $str_ = substr($v["v_path"], -1);
                        if($str_ == '\\'){
                            $fpath1 = $v["v_path"].$VideoRs["localfilename"];
                            $fpath2 = $v["v_path"].$VideoRs["remotefilename"];
                        }
                        else{
                            $fpath1 = $v["v_path"] ."\\".$VideoRs["localfilename"];
                            $fpath2 = $v["v_path"] ."\\".$VideoRs["remotefilename"];
                        }
                        //echo $fpath1."<br>".$fpath2."<br>";
                        if(file_exists($fpath1) && is_file($fpath1)) {
                            $fname = $VideoRs["localfilename"];
                            $csv = $rows;
                            $fnamearr = explode('.',$fname);
                            //$zip->addFile($fpath1,$fnamearr[0].".".$fnamearr[1]);
                            $zip->addFile($fpath1, $fname);
                            fwrite($fp, implode(",", $v) . "\r\n");
                            $n++;
                        }
                        elseif($VideoRs["localfilename"]){
                            $msg.=(++$lost).")：".$fpath1.".<br />";
                        }
                        if(file_exists($fpath2) && is_file($fpath2)) {
                            $fname = $VideoRs["remotefilename"];
                            $csv = $rows;
                            $fnamearr = explode('.',$fname);

                            //$zip->addFile($fpath1,$fnamearr[0].".".$fnamearr[1]);
                            $zip->addFile($fpath2, $fname);
                            fwrite($fp, implode(",", $v) . "\r\n");
                            $n++;
                        }
                        elseif($VideoRs["remotefilename"]){
                            $msg.=(++$lost).")：".$fpath2.".<br />";
                        }else{

                        }
                    }
                    else{
                        $retMsg .= "<hr /><div style=\"color:#f00;\">部分记录没有录像</div>";
                    }
                }
            }
            else
            {
                $sql=str_replace("tab_rec_cdrinfo","tab_rec_bakinfo",$sql);
                $rs=M()->query($sql);
                if($rs){
                    foreach ($rs as $k=>$v)
                    {
                        $RecID = getVideoByRecID($v['n_sn'],$type = 2);
                        if($RecID){
                            $sqlVideo="(select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_cdrinfo where `RecID`='$RecID') union (select `RecID`,`LocalFileName`,`RemoteFileName` from tab_ved_bakinfo where `RecID`='$RecID') order by `recid`";//获取录象记录

                            $rs = M()->query($sqlVideo);
                            $VideoRs = $rs[0];
                            $str_ = substr($v["v_path"], -1);
                            if($str_ == '\\'){
                                $fpath1 = $v["v_path"].$VideoRs["localfilename"];
                                $fpath2 = $v["v_path"].$VideoRs["remotefilename"];
                            }
                            else{
                                $fpath1 = $v["v_path"] ."\\".$VideoRs["localfilename"];
                                $fpath2 = $v["v_path"] ."\\".$VideoRs["remotefilename"];
                            }
                            //echo $fpath1."=============".$fpath2;
                            if(file_exists($fpath1) && is_file($fpath1)) {
                                $fname = $v["localfilename"];

                                $fnamearr = explode('.',$fname);

                                //$zip->addFile($fpath1,$fnamearr[0].".".$fnamearr[1]);
                                $zip->addFile($fpath1, $fname);
                                fwrite($fp, implode(",", $v) . "\r\n");
                                $n++;
                            }
                            elseif($VideoRs["localfilename"]){
                                $msg.=(++$lost).")：".$fpath1.".<br />";
                            }
                            if(file_exists($fpath2) && is_file($fpath2)) {
                                $fname = $v["remotefilename"];

                                $fnamearr = explode('.',$fname);

                                //$zip->addFile($fpath2,$fnamearr[0].".".$fnamearr[1]);
                                $zip->addFile($fpath2, $fname);
                                fwrite($fp, implode(",", $v) . "\r\n");
                                $n++;
                            }
                            elseif($VideoRs["remotefilename"]){
                                $msg.=(++$lost).")：".$fpath2.".<br />";
                            }else{}
                        }else{
                            $retMsg .= "<hr /><div style=\"color:#f00;\">部分记录没有录像</div>";
                        }
                    }
                }
            }

            fclose($fp);
            if(file_exists("./Uploads/".$csvName)){
                $zip->addFile("./Uploads/".$csvName,$csvName);
            }
            $zip->close();
            unlink("./Uploads/".$csvName);
            if($n!=0){
                $retMsg .= "<div style=\"color:red;font-size:14px;\">压缩完成，";
                $retMsg .= "共压缩了: $n 个文件，点击下载:<a target='_blank' href='/Uploads/$zipName' style='color:#00f'>$zipName</a></div>";
                if($msg!=""){
                    $retMsg .= "<hr /><div style=\"color:#f00;\">";
                    $retMsg .= "部分录像文件丢失，可能已被删除或转移到其他目录。以下为丢失文件列表</div>{$msg}";
                }
            }else{
                $retMsg .= "下载失败，所选文件已被删除或转移到其他目录";
            }

        }
        return $retMsg;
    }
    //根据ID串，获取相应文件并打包
    //需要php.ini中  zlib.output_compression = On    如果是php5.6以下要启用extension=php_zip.dll，php7已经集成zip扩展
    public function CreateZip($idArr){
        $zip=new \ZipArchive();
        $zipName = date("Ymdihs")."_".$_SESSION["uName"].".zip";
        $csvName = date("Ymdihs")."_".$_SESSION["uName"].".csv";
        $filename = "./Uploads/".$zipName;
        $retMsg = "";
        if($zip->open($filename, \ZIPARCHIVE::CREATE)!==TRUE) {
            $retMsg .= "无法创建 {$filename}";
        }else{
            //注：需要在打包时同时生成CSV文件压缩在里面，所以需要的信息比较多。
            //需要调整csv文件以配合本地播放软件时，调整此字段($sel值)即可。
            //v_path,v_netpath,v_voicefile是必选字段
            $sel="n_sn,n_channelid,d_starttime,d_stoptime,v_voicefile,n_fileformat,n_calldirection,";
            $sel.="v_caller,v_called,v_ext,v_diverter,v_diverted,v_path,v_netpath,n_istalk,remark";
            $lost=0;
            $fp=fopen("./Uploads/".$csvName,"w");
            $csv = array();
            foreach ($idArr as $v){
                $rows = M("rec_cdrinfo")->where("N_SN = {$v}")->field($sel)->find();

                if($rows){
                    $str_ = substr($rows["v_path"], -1);
                    if($str_ == '\\'){
                        $fpath = $rows["v_path"] . $rows["v_voicefile"];
                    }
                    else{
                        $fpath = $rows["v_path"] ."\\". $rows["v_voicefile"];
                    }

                    if(file_exists($fpath)) {
                        $fname = $rows["v_voicefile"];
                        //$v_caller_name = getNameByPhoneNum($rows["v_caller"]);
                        //$v_caller_name = empty($v_caller_name)?'null':$v_caller_name;
                        //$v_called_name = getNameByPhoneNum($rows["v_called"]);
                        //$v_called_name = empty($v_called_name)?'null':$v_called_name;
                        $v_caller = empty($rows["v_caller"])?'null':$rows["v_caller"];
                        $v_called = empty($v["v_called"])?'null':$rows["v_called"];
                        $fnamearr = explode('.',$fname);

                        $zip->addFile($fpath,$fnamearr[0].".".$fnamearr[1]);

                        fwrite($fp, implode(",", $rows) . "\r\n");
                        $n++;
                    }
                    else{
                        $msg.=(++$lost).")：".$fpath.".<br />";
                    }
                }
                else
                {
                    //$sql=str_replace("tab_rec_cdrinfo","tab_rec_bakinfo",$sql);
                    $rows = M("rec_bakinfo")->where("N_SN = {$v}")->field($sel)->find();

                    if($rows){
                        $str_ = substr($rows["v_path"], -1);
                        if($str_ == '\\'){
                            $fpath = $rows["v_path"] . $rows["v_voicefile"];
                        }
                        else{
                            $fpath = $rows["v_path"] ."\\". $rows["v_voicefile"];
                        }

                        if(file_exists($fpath)) {
                            $fname = $rows["v_voicefile"];
                            //$v_caller_name = getNameByPhoneNum($rows["v_caller"]);
                            //$v_caller_name = empty($v_caller_name)?'null':$v_caller_name;
                            //$v_called_name = getNameByPhoneNum($rows["v_called"]);
                            //$v_called_name = empty($v_called_name)?'null':$v_called_name;
                            $v_caller = empty($rows["v_caller"])?'null':$rows["v_caller"];
                            $v_called = empty($rows["v_called"])?'null':$rows["v_called"];
                            $csv = $rows;
                            $fnamearr = explode('.',$fname);

                            $zip->addFile($fpath,$fnamearr[0].".".$fnamearr[1]);
                            fwrite($fp, implode(",", $rows) . "\r\n");
                            $n++;
                        }
                        else{
                            $msg.=(++$lost).")：".$fpath.".<br />";
                        }
                    }
                }
                unset($rows);
            }


            fclose($fp);
            $msg="";//记录下载详细信息;

            if(file_exists("./Uploads/".$csvName)){
                $zip->addFile("./Uploads/".$csvName,$csvName);
            }
            $zip->close();
            unset($FileInfo,$Paths,$csv);


            unlink("./Uploads/".$csvName);
            if($n!=0){
                $retMsg .= "<div style=\"color:red;font-size:14px;\">压缩完成，";
                $retMsg .= "共压缩了: $n 个文件，点击下载:<a target='_blank' href='/Uploads/$zipName' style='color:#00f'>$zipName</a></div>";
                if($msg != ""){
                    $retMsg .= "<hr /><div style=\"color:#f00;\">";
                    $retMsg .= "部分录音文件丢失，可能已被删除或转移到其他目录。以下为丢失文件列表</div>{$msg}";
                }
            }else{
                $retMsg .= "下载失败，所选文件已被删除或转移到其他目录";
            }

        }
        return $retMsg;
    }


    //根据ID串获取录音文件地址
    //返回数组
    public function getPath($where){
        //注：需要在打包时同时生成CSV文件压缩在里面，所以需要的信息比较多。
        //需要调整csv文件以配合本地播放软件时，调整此字段($sel值)即可。
        //v_path,v_netpath,v_voicefile是必选字段
        $sel="n_sn,n_channelid,d_starttime,d_stoptime,v_voicefile,n_fileformat,n_calldirection,";
        $sel.="v_caller,v_called,v_ext,v_diverter,v_diverted,v_path,v_netpath,n_istalk,remark";
        $sql="select {$sel} from tab_rec_cdrinfo ".$where;

        $csv = array();
        $path = array();
        $rows=M()->query($sql);
        $v_caller = '';
        $v_called = '';
        if($rows){
            foreach ($rows as $k=>$v)
            {
                $fpath=$v["v_path"].$v["v_voicefile"];
                $fname=$v["v_voicefile"];
                $v_caller = $v["v_caller"];
                $v_called = $v["v_called"];
                $n_channelid = $v["n_channelid"];
                $csv=$rows;
                $path[]=array("fpath"=>"$fpath","fname"=>"$fname","v_caller"=>$v_caller,"v_called"=>$v_called,"n_channelid" => $v["n_channelid"]);
            }
        }
        else
        {
            $sql=str_replace("tab_rec_cdrinfo","tab_rec_bakinfo",$sql);
            $rs=M()->query($sql);
            if($rs){
                foreach ($rs as $k=>$v)
                {
                    $fpath=$v["v_path"].$v["v_voicefile"];
                    $fname=$v["v_voicefile"];
                    $v_caller = $v["v_caller"];
                    $v_called = $v["v_called"];
                    $n_channelid = $v["n_channelid"];
                    $csv=$rs;
                    $path[]=array("fpath"=>"$fpath","fname"=>"$fname","v_caller"=>$v_caller,"v_called"=>$v_called,"n_channelid" => $v["n_channelid"]);
                }
            }
        }
        //返回数组，包含2个元素，file包括文件信息，csv包括要生成CSV文件的内容。都是数组格式
        $aryReturn=array("file"=>$path,"csv"=>$csv);
        return $aryReturn;
    }


    //统计打印
    public function printPage()
    {
        $s_time = date("Y-m-d",strtotime("-300 day"))." 00:00:00";
        $e_time = date("Y-m-d",time())." 23:59:59";
        $SearchMode = isset($_REQUEST['SearchMode'])?$_REQUEST['SearchMode']:1;

        if(IS_AJAX){
            $sEcho = $_REQUEST['sEcho']; // DataTables 用来生成的信息
            $output['sEcho'] = $sEcho;
            $start = isset($_REQUEST['start'])?$_REQUEST['start']:0;
            $length = isset($_REQUEST['length'])?$_REQUEST['length']:10;
            $datetimepicker_start = isset($_REQUEST['datetimepicker_start'])?$_REQUEST['datetimepicker_start']:$s_time;
            $datetimepicker_end = isset($_REQUEST['datetimepicker_end'])?$_REQUEST['datetimepicker_end']:$e_time;



            //如没设定开始日期，则默认为上个月的今天
            $bTime=isset($_POST["bTime"])?$_POST["bTime"]:$lastmonth = date("Y-m-d",mktime(0,0,0,date("m")-1 ,date("d"),date("Y")));
            //如没设定开始日期，则默认为昨天
            $eTime=isset($_POST["eTime"])?$_POST["eTime"]:date("Y-m-d",mktime(0,0,0,date("m") ,date("d")-1,date("Y")));

            $where  = " D_StartTime >= '{$datetimepicker_start}' and D_StartTime <= '{$datetimepicker_end}' ";
            $where  .= " and D_StopTime != '0000-00-00 00:00:00' ";

            if($SearchMode == 1)
            {

                $field = "N_ChannelID,Count(N_SN) as 'cAmount',Sum(ABS(TIMESTAMPDIFF(SECOND,D_StartTime,D_StopTime))) as cSecond";
                $cnt = M('rec_cdrinfo')->field("N_ChannelID ")->where($where)->group('N_ChannelID')->select();
                $total = count($cnt);

                $rs = M('rec_cdrinfo')->field($field)->where($where)->group('N_ChannelID')->limit($start,$length)->select();

                if($rs){
                    foreach ($rs as $k=>$v){
                        $rs[$k]['recordtime'] = formatTime($v["csecond"]);
                        $data = $this->videoSimpleCount($v['n_channelid'],$where);
                        $rs[$k]['vtimes'] = $data['vtimes'];
                        $rs[$k]['vsecond'] = $data['vsecond'];
                    }
                }

            }
            else
            {

                $field = "*";
                $cnt = M('rec_cdrinfo')->field("count(*) cnt ")->where($where)->find();
                $total = $cnt['cnt'];
                $rs = M('rec_cdrinfo')->field($field)->where($where)->limit($start,$length)->select();

                if($rs){
                    foreach ($rs as $k=>$v){
                        $rs[$k]['longtime'] = DateDiff($v['d_starttime'],$v['d_stoptime']);
                        $rs[$k]['n_calldirection'] = $v['n_calldirection']==1?'拨出':'来电';
                    }
                }
            }

            $output['aaData'] = $rs;
            $output['iTotalDisplayRecords'] = $total;    //如果有全局搜索，搜索出来的个数
            $output['iTotalRecords'] = $total; //总共有几条数据
            echo json_encode($output); //最后把数据以json格式返回
            die;
        }
        $this->assign("SearchMode",$SearchMode);
        $this->assign("datetimepicker_start",$s_time);
        $this->assign("datetimepicker_end",$e_time);
        $this->assign("CurrentPage",'recordCount');
        $this->display();
    }

    public function downloadRecordExcel(){
        $start = isset($_REQUEST['start'])?$_REQUEST['start']:0;
        $length = isset($_REQUEST['length'])?$_REQUEST['length']:10;

        $SearchMode = isset($_REQUEST['SearchMode'])?$_REQUEST['SearchMode']:0;
        $search_key = $_REQUEST['search_key'];
        $has_video = $_REQUEST['has_video'];
        $stime = isset($_REQUEST['stime'])?$_REQUEST['stime']:1;
        $InOut = $_REQUEST['InOut'] == ''?2:$_REQUEST['InOut'];
        $chk_CN = $_REQUEST['chk_CN'];
        $datetimepicker_start = isset($_REQUEST['datetimepicker_start'])?$_REQUEST['datetimepicker_start']:$s_time;
        $datetimepicker_end = isset($_REQUEST['datetimepicker_end'])?$_REQUEST['datetimepicker_end']:$e_time;

        $m = M("rec_cdrinfo");
        $m_bak = M("rec_bakinfo");

        $field="N_SN,N_ChannelID,D_StartTime,D_StopTime,V_VoiceFile,N_FileFormat,";
        $field.="N_CallDirection,V_Caller,V_Called,V_Ext,N_BackUp,N_Lock,N_Alarm,";
        $field.="N_AvoidRec,N_DeleteFlag,N_Locker,V_Path,V_NetPath,";
        $field.="V_Diverter,V_Diverted,N_IsTalk,ReMark ";


        $where  .=  " D_StartTime between '{$datetimepicker_start}' and '$datetimepicker_end' ";

        if(!empty($chk_CN)){
            $where  .= " and n_sn in ($chk_CN)";
        }

        if($search_key != ""){//通话号码(姓名)的取值，0则号码，1则姓名
            if($SearchMode==0){
                $numArr=getNumByName($search_key);
                if(!$numArr || empty($numArr)){
                    $where .= ' and N_SN = -1';//姓名没匹配到号码，直接设置一个不成立的条件
                }else{
                    $where  .= " and (";
                    foreach($numArr as $k=>$v){
                        if($k == 0){
                            $where  .= "   v_caller  = '{$v}' or v_called  = '{$v}' or v_ext = '{$v}' ";
                        }
                        else{
                            $where  .= " or  v_caller  = '{$v}' or v_called  = '{$v}'  or v_ext = '{$v}' ";
                        }

                    }
                    $where  .= ")";
                }
            }else{
                $where .= " and (V_caller like '".$search_key."%' or V_called like '".$search_key."%'  orv_ext like '".$search_key."%')";
            }
        }
        if ($has_video==1) {//只查有录象的记录
            $sql = "(select `recid` from tab_ved_cdrinfo) union (select `recid` from tab_ved_bakinfo) order by `recid` ";
            $res = M()->query($sql);
            $recid = "";
            if($res){
                foreach ($res as $v){
                    if($recid == ""){
                        $recid = $v['recid'];
                    }
                    else
                    {
                        $recid = $recid .",".$v['recid'];
                    }
                }
            }
            if(!empty($recid)) $where .= " and N_SN in ($recid)";
        }

        //按拨叫方向查询
        if($InOut!=2){
            $where .= " and N_CallDirection= {$InOut}";
        }
        $where.=" and  (unix_timestamp(D_StopTime)-unix_timestamp(D_StartTime))>=$stime ";
        $CntNoBackup=$m->field("count(*) cnt")->where($where)->find();  //未备份部分数据
        $CntNoBackup=$CntNoBackup['cnt'];
        $CntBackup = $m_bak->field("count(*) cnt")->where($where)->find();  //已备份部分数据
        $CntBackup=$CntBackup['cnt'];
        $total = (int)$CntNoBackup + (int)$CntBackup;

        $rs = array();
        if($CntBackup==0)
        {
            //无已备份数据，读取未备份数据即可
            $rs= $m->field($field)->where($where)->order("D_StartTime desc")->select();
        }
        else
        {
            if($CntNoBackup==0)
            {
                //有已备份数据，无未备份数据。读取已备份份数据即可
                $rs= $m_bak->field($field)->where($where)->order("D_StartTime desc")->select();
            }
            else
            {
                //未备份和已备份都有，需要读取两张表
                //计算未备份数据有多少页$NotBak_pagecnt
                //未备份数据不足一页的数据，需要同时读取备份与未备份数据
                $rsNoBak= $m->field($field)->where($where)->order("D_StartTime desc")->select();
                $rsBak= $m_bak->field($field)->where($where)->order("D_StartTime desc")->select();
                $rs= array_merge($rsNoBak,$rsBak);
            }
        }

        if($rs){
            foreach ($rs as $k=>$v)
            {
                $fileplay ="<i class='fa fa-volume-up fa-2x' style=\"cursor:pointer\" onclick=\"play('".$v["n_sn"]."','".$v["v_voicefile"]."')\"></i>";

                $rs[$k]['n_channelinfo'] = getChannelNameById($v['n_channelid']);
                $rs[$k]['v_caller'] = getNameByPhoneNum($v['v_caller']);
                $rs[$k]['v_called'] = getNameByPhoneNum($v['v_called']);
                $rs[$k]['v_ext'] = getNameByPhoneNum($v['v_ext']);
                $rs[$k]['n_calldirection'] = $v['n_calldirection']==1?'拨出':'来电';
                $rs[$k]['longtime'] = DateDiff($v['d_starttime'],$v['d_stoptime']);
                $rs[$k]['v_voicefileplay'] = $fileplay;
                $rs[$k]['local_video'] = getVideoByRecID($v['n_sn'],0);
                $rs[$k]['remote_video'] = getVideoByRecID($v['n_sn'],1);
            }
        }

        header("Content-type: text/html; charset=utf-8");
        Vendor("PHPExcel.PHPExcel");
        Vendor("PHPExcel.PHPExcel.Reader.Excel2007.php");
        Vendor("PHPExcel.PHPExcel.Reader.Excel5");
        Vendor("PHPExcel.PHPExcel.IOFactory");

        $excel = new \PHPExcel();

        //指定工作簿
        $excel->setActiveSheetIndex(0);
        //设置列宽
        $excel->getActiveSheet()->getColumnDimension('A')->setWidth('5');
        $excel->getActiveSheet()->getColumnDimension('B')->setWidth('5');
        $excel->getActiveSheet()->getColumnDimension('C')->setWidth('10');
        $excel->getActiveSheet()->getColumnDimension('D')->setWidth('10');
        $excel->getActiveSheet()->getColumnDimension('E')->setWidth('10');
        $excel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $excel->getActiveSheet()->getColumnDimension('G')->setWidth('5');
        $excel->getActiveSheet()->getColumnDimension('H')->setWidth('10');
        $excel->getActiveSheet()->getColumnDimension('I')->setWidth('10');
        $excel->getActiveSheet()->getColumnDimension('J')->setWidth('10');
        $excel->getActiveSheet()->getColumnDimension('K')->setWidth('5');
        $excel->getActiveSheet()->getColumnDimension('L')->setWidth('5');


        $excel->getActiveSheet()->setTitle('录音记录'); //excel标题
        //设置行标题
        $excel->getActiveSheet()->setCellValue("A1", "ID");
        $excel->getActiveSheet()->setCellValue("B1", "线路号");
        $excel->getActiveSheet()->setCellValue("C1", "线路信息");
        $excel->getActiveSheet()->setCellValue("D1", "主叫");
        $excel->getActiveSheet()->setCellValue("E1", "被叫");
        $excel->getActiveSheet()->setCellValue("F1", "录音号码");
        $excel->getActiveSheet()->setCellValue("G1", "方向");
        $excel->getActiveSheet()->setCellValue("H1", "开始时间");
        $excel->getActiveSheet()->setCellValue("I1", "结束时间");
        $excel->getActiveSheet()->setCellValue("J1", "时长");
        $excel->getActiveSheet()->setCellValue("K1", "锁定");
        $excel->getActiveSheet()->setCellValue("L1", "备注");


        //获取并填充数据
        if($rs){
            $i=1;
            foreach ($rs as $key => $value) {
                $i++;
                $excel->getActiveSheet()->setCellValue("A".$i,$value["n_sn"]);
                $excel->getActiveSheet()->setCellValue("B".$i,$value["n_channelid"]);
                $excel->getActiveSheet()->setCellValue("C".$i,$value["n_channelinfo"]);
                $excel->getActiveSheet()->setCellValue("D".$i,$value["v_caller"]);
                $excel->getActiveSheet()->setCellValue("E".$i,$value["v_called"]);
                $excel->getActiveSheet()->setCellValue("F".$i,$value["v_ext"]);
                $excel->getActiveSheet()->setCellValue("G".$i,$value["n_calldirection"]);
                $excel->getActiveSheet()->setCellValue("H".$i,$value["d_starttime"]);
                $excel->getActiveSheet()->setCellValue("I".$i,$value["d_stoptime"]);
                $excel->getActiveSheet()->setCellValue("J".$i,$value["longtime"]);
                $excel->getActiveSheet()->setCellValue("K".$i,$value["n_lock"]);
                $excel->getActiveSheet()->setCellValue("K".$i,$value["remark"]);
            }
        }


        $title ='recordExcel';
        $date = date('Y-m-d',time());

        $data = array();
        $fileurl = "Uploads/records/". $title."_".$date.".xls";
        mkdir_for_url($fileurl);
        $filename = iconv('utf-8', "gb2312", $fileurl);

        $objwriter = \PHPExcel_IOFactory::createWriter($excel,'Excel5');
        $objwriter->save($filename);
        $this->returnJSON(1,'导出成功',"/".$fileurl);

    }

    public function test(){
        $this->display();
    }
}
